Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

python - Page not found (404) when submitting a form in 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>

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!

question from:https://stackoverflow.com/questions/65641696/django-page-not-found-404-forms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

e.g:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...