Christopher Spears
Christopher Spears

Reputation: 1105

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!

Upvotes: 1

Views: 7409

Answers (3)

OllyTheNinja
OllyTheNinja

Reputation: 566

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">

Upvotes: 7

aashnisshah
aashnisshah

Reputation: 476

It seems like the issue is with your URLs.py page - it seems like you haven't setup the url rewriting section of django

Upvotes: 0

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83768

Most likely the <form action> attribute is wrong.

Now it points relatively to:

   rango/register/rango/register/

   <form id="user_form" method="post" action="rango/register/" <--- relative

For a quick workaround try (not a best practice):

   <form id="user_form" method="post" action="/rango/register/" 

instead.

Learn how relative URL works within HTML pages and this should unravel the mystery.

Upvotes: 0

Related Questions