在Django中提交表单时找不到页面(404) [英] Page not found (404) when submitting a form in Django

查看:99
本文介绍了在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:    http://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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆