Django 1.8
项目名:Yiyipin
项目应用:Reading
/root/Myproject/Yiyipin/Yiyipin:项目下的urls.py(与manage.py在同一个目录下)
1.urls.py:
urlpatterns += [
url(r'^Reading/', include('Reading.urls', namespace='Reading')),
]
/root/Myproject/Yiyipin/Yiyipin/Reading:
2.项目应用下的urls.py:
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
urlpatterns = patterns(
'Reading.views',
url(r'^profile/',TemplateView.as_view(template_name = 'profile.html')),
url(r'^reading/', 'SaveProfile', name = 'reading')
)
3.模板文件:profile.html
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form name = "form" enctype = "multipart/form-data" action = **"{% url "Reading.views.SaveProfile" %}"** method = "POST" >{% csrf_token %}
<div style = "max-width:470px;">
<center>
<input type = "text" style = "margin-left:20%;"
placeholder = "书名" name = "Bookname" />
</center>
</div>
<br>
......
4.视图:views.py
from Reading.forms import ProfileForm
from Reading.models import Profile
def SaveProfile(request):
saved = False
if request.method == "POST":
MyProfileForm = ProfileForm(request.POST, request.FILES)#Get the posted form
if MyProfileForm.is_valid():
reading = Profile()
reading.Bookname = MyProfileForm.cleaned_data["Bookname"]
reading.picture = MyProfileForm.cleaned_data["picture"]
reading.save()
saved = True
else:
MyProfileForm = Profileform()
return HttpResponseRedirect(reverse_lazy("reading"))
return render(request, 'readingList.html', locals())
在浏览器中输入网址:http://39.108.172.192:8000/Reading/profile/
结果如下:
请问这是什么原因,谢谢
Django中的超链接目标地址可以用" {% url 'app_name:url_name' param %} "(其中app_name[应用名]和url_name[url名称]都在url中配置),就比如

