找不到网址

时间:2017-01-31 14:10:37

标签: python django

我收到了一个错误,

Page not found (404)
Request Method: GET
Request URL:    `http://localhost:8000/accounts/registration/accounts/registration/accounts/registration/accounts/profile.html`   .

我认为路线错了但是我无法理解如何修理路线。

在帐户应用中,我写道 在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'),
]
在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)

在帐户(子应用程序)/templates/registration/accounts/profile.html目录中,

{% 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 %}

1 个答案:

答案 0 :(得分:1)

你在这里有一些严重的误解。

没有视图就无法拥有模板。您已为配置文件编写了模板,但尚未编写视图。您需要加载配置文件数据的视图,然后呈现profile.html模板。

其次,您的网址与模板位置无关;正如您在regist_save中所做的那样,您应该定义一个指向该视图的合理URL - 对于该配置文件,您可能需要r'^profile/$'之类的内容。

所以,你的urls.py中的第五个条目应该是:

url(r'^profile/$', views.profile, name='profile'),

并且您需要在views.py中使用名为profile的相应函数。

最后,当您重定向时,您需要使用实际的URL条目 - 再次,它与模板无关。因此,在regist_save视图中,您应该执行以下操作:

return redirect('profile')