使用django-registration表单而不是自己的

时间:2016-09-28 20:41:21

标签: python django forms django-registration

所以我使用django-registration进行电子邮件激活等。我定义了我的表单,并正在调用它,但由于某种原因它使用django-registration而不是我自己的基本表单。我完全不知道为什么会这样,有什么想法吗?

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

模板

{% 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 %}

1 个答案:

答案 0 :(得分:1)

由于urlpatterns具有该顺序:

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'^accounts/', include('registration.backends.hmac.urls')),的网址首先与您的/accounts/register/网址匹配,并且无法访问您的views.register视图。因此,要更改它,您需要更改顺序,以便网址模式为

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