1

I am trying to save post data for a custom user registration form but I can't identify where I am going wrong. I am suspecting the problem is in mainapp/urls. I have done some research on the subject read the following documentation,Question on StackOverflow,Question on StackOverflow and many others but I cannot find the problem.

template

{% extends 'base.html'%}
{% load staticfiles %}
{% block content%}
{% load bootstrap %}

<div class = "signup-box">
    <h2>Sign up</h2>
    <form class="form-horizontal" action="signup" method="post">
        {% csrf_token %}
        <!-- {{ form|bootstrap }} -->

        {{ form.username.label }}
        {{ form.username }}
        {{ form.first_name.label }}
        {{ form.first_name }}
        {{ form.last_name.label }}
        {{ form.last_name }}
        {{ form.email.label }}
        {{ form.email }}
        {{ form.password1.label }}
        {{ form.password1 }}
        {{ form.password2.label }}
        {{ form.password2 }}<br>    
        <input type="submit" value="Submit" />
    </form>
</div>

{% endblock  %}

urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^$', views.home),
    url(r'^login/$', views.login, name='login'),
    url(r'^signup/$', views.signup, name='signup'),

]

Views.py

from django.shortcuts import render, redirect
from .forms import RegistrationForm
from .models import User
from django.contrib import messages
from django.http import HttpResponse
# Create your views here.

def register_page(request):
    if request.method == "POST":
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponse('<h1>The form is valid!<h1>')
        else:
            form = RegistrationForm(request.POST)
            args = {'form':form}
            return render(request, 'signup', args)
    else:
        form = RegistrationForm()
        return render(request, 'signup.html', {'form': form})

I get the following error after submitting the form

The current path, register_page/register_page/signup, didn't match any of these.
Confusion Matrix
  • 116
  • 2
  • 14
  • so whats the problem? whats not working? – N. Ivanov Sep 22 '17 at 14:16
  • Does your `if request.method == 'POST'` enter? Does your form pass vaildation... Have you printed out some values to see isolate where the error is? – Jon Clements Sep 22 '17 at 14:17
  • I cant tell if the post method passes the validation because I suspect the error is in my urls.py file. – Confusion Matrix Sep 22 '17 at 14:25
  • 1
    I don't know why you've posted all that irrelevant information when you know the problem is with the URLs. – Daniel Roseman Sep 22 '17 at 14:25
  • For anyone who feels I have not done enough research please recommend a resource that would help me. I just suspected the problem was with my urls, but don't know why. Perhaps the problem could have been elsewhere. – Confusion Matrix Sep 22 '17 at 14:27

1 Answers1

1

The problem is the URL you are posting to. The action of your form is a relative path, "signup"; because it has no leading slash it will always be appended to the current path.

You could fix this by prepending a slash, but you should actually not do this; you should post to the same URL as you came from, so action=".".

Note, there are various other errors with your code, not least the fact that you unnecessarily re-instantiate the form if it fails validation, so you will never see any validation errors.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895