在Django中提交表单时找不到页面(404)

编程入门 行业动态 更新时间:2024-10-13 16:16:19
本文介绍了在Django中提交表单时找不到页面(404)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究《 Tango With Django》教程.我正在尝试允许用户向该应用程序注册.但是,当用户按下提交"按钮时,出现以下错误消息:

I am working through the Tango With Django tutorial. I am trying to allow the user to register with the app. However, when the user presses the submit button, I get this error message:

Page not found (404) Request Method: POST Request URL: 127.0.0.1:8000/rango/register/rango/register/ Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order: ^admin/ ^rango/ ^$ [name='index'] ^rango/ ^about$ [name='about'] ^rango/ ^add_category/$ [name='add_category'] ^rango/ ^category/(?P<category_name_url>\w+)/$ [name='category'] ^rango/ ^category/(?P<category_name_url>\w+)/add_page/$ [name='add_page'] ^rango/ ^register/$ [name='register'] media/(?P<path>.*) The current URL, rango/register/rango/register/, didn't match any of these.

我不确定该如何构建怪异的路径.这是注册模板:

I'm not sure how that weird path is being built. Here is registration template:

<!DOCTYPE html> <html> <head> <title>Rango</title> </head> <body> <h1>Register with Rango</h1> {% if registered %} Rango says: <strong>thank you for registering!</strong> <a href="/rango/">Return to the homepage.</a><br /> {% else %} Rango says: <strong>register here!</strong><br /> <form id="user_form" method="post" action="rango/register/" enctype="multipart/form-data"> {% csrf_token %} {{ user_form.as_p }} {{ profile_form.as_p }} <input type="submit" name="submit" value="Register" /> </form> {% endif %} </body> </html>

为了进入注册模板,您需要在索引模板中单击以下链接:

In order to get to the registration template, you need to click this link in the index template:

<a href="/rango/register">Register Here</a>

这是rango/views.py中的注册功能:

Here is the register function in rango/views.py:

def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})

我确定我缺少一些小东西!

I am sure I am missing something small!

推荐答案

当前,您的浏览器将"rango/register/" 添加到当前URL的末尾.如果将其更改为"./" 或"/rango/register/" ,它会指向自身,但这并不是最佳实践.

Currently you browser adds "rango/register/" to the end of the current URL. If you changed it to "./" or "/rango/register/" it would point to itself, however these are not best practise.

为获得最佳实践,请使用 {%url"register"%} ,那样,当您更改url.py时,它将自动更改.

For best practise use {% url "register" %} instead, that way it will automatically change is you change your url.py

例如:

<form id="user_form" method="post" action="{% url "register" %}" enctype="multipart/form-data">

更多推荐

在Django中提交表单时找不到页面(404)

本文发布于:2023-11-15 10:14:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   表单   页面   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!