So I am using django-registration
for email activation and the like. I defined my form and am calling to it properly however for some reason it is using the base form out of django-registration
instead of my own. I have absolutely no clue as to why this is happening, any thoughts?
views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from django.template.context_processors import csrf
from django.contrib.auth.models import User
from .forms import UserForm
def register(request):
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register/complete')
else:
form = UserForm()
token = {}
token.update(csrf(request))
token['form'] = form
return render('registration/registration_form.html', token)
def registration_complete(request):
return render('registration/registration_complete.html')
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from main import views
urlpatterns = [
# Registration URLs
url(r'^accounts/', include('registration.backends.hmac.urls')),
url(r'^accounts/register/$', views.register, name='register'),
url(r'^accounts/register/complete/$', views.registration_complete, name='registration_complete'),
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^login/$', views.login, name='login'),
url(r'^register/$', views.register, name='register'),
url(r'^profile/$', views.profile, name='profile'),
url(r'^blog/$', views.blog, name='blog'),
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.utils.translation import gettext as _
from registration.forms import RegistrationFormUniqueEmail, RegistrationForm
class UserForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(label=_("Firstname"), required=False)
last_name = forms.CharField(label=_("Lastname"), required=False)
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
def save(self, commit=True):
user = super(UserForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
template
{% extends "base.html" %}
{% block title %}Register{% endblock %}
{% block content %}
<h2>Registration</h2>
<form action="/accounts/register/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Register" />
</form>
{% endblock %}
Since urlpatterns
has that order:
url(r'^accounts/', include('registration.backends.hmac.urls')),
url(r'^accounts/register/$', views.register, name='register'),
url(r'^accounts/register/complete/$', views.registration_complete, name='registration_complete'),
Url from url(r'^accounts/', include('registration.backends.hmac.urls')),
match your /accounts/register/
url first, and don't get to your views.register
view. So, to change that, you need to change the order, so that url patterns would be
urlpatterns = [
url(r'^accounts/register/$', views.register, name='register'),
url(r'^accounts/register/complete/$', views.registration_complete, name='registration_complete'),
url(r'^accounts/', include('registration.backends.hmac.urls')),
...
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.