python - Cannot Redirect to URL after form post in django -
i creating user registration system in django1.8. when click register button on form, not redirect success url. not sure if right way save user information in database. please recommend if there better way approach user registration in django.
models.py
from django.db import models django.contrib.auth.models import user class userprofile(models.model): user = models.onetoonefield(user) age = models.integerfield(blank=true) gender_choices = (('m', 'male'), ('f', 'female')) gender = models.charfield(max_length=1, choices=gender_choices, default='male')
forms.py
from django import forms .models import userprofile class userprofileform(forms.modelform): class meta: model = userprofile exclude = ['user']
views.py
from django.shortcuts import render, redirect django.core.urlresolvers import reverse django.contrib.auth.forms import usercreationform django.contrib.auth.models import user .forms import userprofileform # create views here. def registerview(request): if request.method == "post": user_form = usercreationform(request.post) user_profile_form = userprofileform(request.post) if user_form.is_valid() , user_profile_form.is_valid(): new_user = user_form.save() new_user_profile = user_profile_form.save() return redirect(reverse('success')) else: return render(request, 'register.html', {'user_form': user_form, 'user_profile_form': user_profile_form}) else: user_form = usercreationform(request.post) user_profile_form = userprofileform(request.post) return render(request, 'register.html', {'user_form': user_form, 'user_profile_form': user_profile_form}) def successview(request): username = user.objects.get('username') return render(request, 'success.html', {'username': username})
urls.py
from django.conf.urls import url . import views urlpatterns = [ url(r'^$', views.registerview, name='register'), url(r'^success/$', views.successview, name='success') ]
register.html
<h1> welcome </h1> <form method="post" action="{% url 'success' %}"> {% csrf_token %} {{ user_form.as_p }} {{ user_profile_form.as_p }} <input type="button" value="register"/> </form>
success.html
<h4> yo mr...{{ username }}</h4> <h1>welcome</h1>
you need make following changes:
<h1> welcome </h1> <form method="post" action="{% url 'register' %}"> {% csrf_token %} {{ user_form.as_p }} {{ user_profile_form.as_p }} <input type="submit" value="register"/> </form>
Comments
Post a Comment