找不到网址 [英] Cannot find url
问题描述
我有一个错误,
找不到页面(404)
请求方法:GET
请求URL:http:// localhost:8000 / accounts / registration / accounts / registration / accounts / registration / accounts / profile.html。
我认为路由错误但是我不明白如何修复路由。
在帐户应用程序中,我在django中的urls.py
中写了
/ pre>
。 conf.urls import url
from。从django.contrib.auth.views导入视图
导入登录,注销
urlpatterns = [
url(r'^ login / $',login,
{ 'template_name':'registration / accounts / login.html'},
name ='login'),
url(r'^ logout / $',logout,name ='logout'),
url(r'^ regist / $',views.regist,name ='regist'),
url(r'^ regist_save / $',views.regist_save,name ='regist_save'),
url(r'^ registration / accounts / registration / accounts / profile.html $',views.regist_save,name ='regist_save'),
]
in views.py
@require_POST
def regist_save(request):
form = RegisterForm(request.POST)
如果form.is_valid():
user = form.save()
login(请求,用户)
context = {
'user':request.user,
}
return redirect('registration / accounts / profile.html',context)
context = {
'form':form,
}
return render(request,'registration / accounts / regist.html',context)
在帐户(子应用)/templates/registration/accounts/profile.html目录中,
{%extendsregistration / accounts / base.html%}
{%block content%}
user.username :{{user.username}}< hr>
user.is_staff:{{user.is_staff}}< hr>
user.is_active:{{user.is_active}}< hr>
user.last_login:{{user.last_login}}< hr>
user.date_joined:{{user.date_joined}}
{%endblock%}
解决方案你在这里有一些严重的误会。
你没有一个视图没有模板。您已为该配置文件编写了一个模板,但您尚未写入视图。您需要加载配置文件数据的视图,然后渲染profile.html模板。
其次,您的URL与模板位置无关;就像您在regist_save中所做的那样,您应该定义一个指向该视图的敏感URL - 对于配置文件,您可能需要像
r'^ profile / $'
。
所以,你的urls.py中的第五个条目应该是:
url(r'^ profile / $',views.profile,name ='profile'),
,并且您需要在views.py中命名为
profile
的相应功能。
最后,当您重定向时,您需要使用实际的URL条目 - 再次,它与模板无关。所以在您的
regist_save
视图中,您应该执行以下操作:返回重定向'profile')
I got an error that,
Page not found (404) Request Method: GET Request URL: `http://localhost:8000/accounts/registration/accounts/registration/accounts/registration/accounts/profile.html` .
I think routes are wrong But I cannot understand how to fix the routes.
In accounts app,I wrote in urls.py
from django.conf.urls import url from . import views from django.contrib.auth.views import login, logout urlpatterns = [ url(r'^login/$', login, {'template_name': 'registration/accounts/login.html'}, name='login'), url(r'^logout/$', logout, name='logout'), url(r'^regist/$', views.regist,name='regist' ), url(r'^regist_save/$', views.regist_save, name='regist_save'), url(r'^registration/accounts/registration/accounts/profile.html$', views.regist_save, name='regist_save'), ]
in views.py
@require_POST def regist_save(request): form = RegisterForm(request.POST) if form.is_valid(): user = form.save() login(request, user) context = { 'user': request.user, } return redirect('registration/accounts/profile.html', context) context = { 'form': form, } return render(request, 'registration/accounts/regist.html', context)
in accounts(child app)/templates/registration/accounts/profile.html directory,
{% extends "registration/accounts/base.html" %} {% block content %} user.username: {{ user.username }}<hr> user.is_staff: {{ user.is_staff }}<hr> user.is_active: {{ user.is_active }}<hr> user.last_login: {{ user.last_login }}<hr> user.date_joined: {{ user.date_joined }} {% endblock %}
解决方案You have some serious misunderstandings here.
You can't have a template without a view. You have written a template for the profile, but you haven't written a view. You need the view that loads the profile data and then renders the profile.html template.
Secondly, your URL has nothing to do with the template location; as you have done in regist_save, you should define a sensible URL pointing to that view - for the profile, you probably want something like
r'^profile/$'
.So, the fifth entry in your urls.py should be:
url(r'^profile/$', views.profile, name='profile'),
and you need a corresponding function named
profile
in views.py.Finally, when you redirect you need to use an actual URL entry - again, it has nothing to do with templates. So in your
regist_save
view, you should do:return redirect('profile')
这篇关于找不到网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!