sikolia Wycliffe sikolia Wycliffe - 5 months ago 37
Python Question

Saving Post data to the database using django

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

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


{% 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.password1.label }}
{{ form.password1 }}
{{ form.password2.label }}
{{ form.password2 }}<br>
<input type="submit" value="Submit" />

{% endblock %}

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'),


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():
return HttpResponse('<h1>The form is valid!<h1>')
return redirect('/login.html')
form = RegistrationForm(request.POST)
args = {'form':form}
return render(request, 'signup', args)
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download