Python-Django:找不到模板

时间:2014-11-03 02:34:40

标签: python django templates url

我是Python和Django的新手,并且一直在努力学习如何为我的网站制作注册表单。我正在关注本教程https://www.youtube.com/watch?v=Tam4IGrPESg。我已经完成了一切工作,直到registration_success页面。以下是我认为的相关代码:

settings.py

TEMPLATE_DIRS = (
    TEMPLATE_PATH + '/templates/',
    TEMPLATE_PATH + '/templates/registration',
)

urls.py

#registration
(r'^register/$', register_user),
(r'^registration_success/$', registration_success),

views.py

def register_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('registration_success')         
    args = {}
    args.update(csrf(request))
    print (args)
    args['form'] = UserCreationForm()

    return render_to_response('register.html',args)

def registration_success(request):
    return render_to_response('registration_success.html')

我得到的错误是:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/register/registration_success
Using the URLconf defined in tut_1.urls, Django tried these URL patterns, in this order:
^$
^login/$
^logout/$
^register/$
^registration_success/$
^portal/
^static/(?P<path>.*)$
The current URL, registration/registration_success, didn't match any of these.

我不确定到底发生了什么。我注意到的是URL是:http:// localhost:8000 / register / registration_success。为什么注册/注册_...?它应该不是注册/ registration_success吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

return HttpResponseRedirect('registration_success') 

您正在重定向到相对于当前网址的路径。这就解释了为什么你被路由到/ register / registration_success。

要解决此问题,您需要使用根相对路径

return HttpResponseRedirect('/registration_success/')