AttributeError:模块Django.contrib.auth.views没有属性 [英] AttributeError: module Django.contrib.auth.views has no attribute

查看:674
本文介绍了AttributeError:模块Django.contrib.auth.views没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django应用程序用户帐户中,我创建了一个Sign-Up表单和一个用于Sign-up的模型.但是,当我去运行python manage.py makemigrations时,遇到错误:AttributeError:模块Django.contrib.auth.views没有属性'registration'.其次,我是否在Forms.py中正确编码了SignUpForm?我不想在模型中使用User模型,因为它将要求用户名,并且我不希望我的网站要求用户名.

In my Django app useraccounts, I created a Sign-Up form and a model for my Sign-up. However, when I went to run python manage.py makemigrations, I encounter the error: AttributeError: module Django.contrib.auth.views has no attribute 'registration'. Secondly, am I coding the SignUpForm in forms.py correctly? I did not want to use the User model in models because it would request username and I didn't want my website to ask for a username.

这是我的代码:

models.py

models.py

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

class UserProfile(models.Model):
    first_name = models.CharField(max_length=150)
    last_name =  models.CharField(max_length=150)
    email = models.EmailField(max_length=150)
    birth_date = models.DateField()
    password = models.CharField(max_length=150)

@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)
    instance.profile.save()

forms.py

from django.forms import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from useraccounts.models import UserProfile

class SignUpForm(UserCreationForm):

    class Meta:
        model = User

        fields = ('first_name',
                  'last_name',
                  'email',
                  'password1',
                  'password2', )

views.py

from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from useraccounts.forms import SignUpForm

# Create your views here.
def home(request):
    return render(request, 'useraccounts/home.html')

def login(request):
    return render(request, 'useraccounts/login.html')

def registration(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            user.refresh_from_db()
            user.profile.birth_date = form.cleaned_data.get('birth_date')
            user.save()
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(password=raw_password)
            login(request, user)
            return redirect('home')
        else:
            form = SignUpForm()
        return render(request, 'registration.html', {'form': form})

urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views

urlpatterns = [

    url(r'^$', views.home),
    url(r'^login/$', auth_views.login, {'template_name': 'useraccounts/login.html'}, name='login'),
    url(r'^logout/$', auth_views.logout, {'template_name': 'useraccounts/logout.html'}, name='logout'),
    url(r'^registration/$', auth_views.registration, {'template_name': 'useraccounts/registration.html'}, name='registration'),

]

推荐答案

打开urls.py并替换:

django.contrib.auth.views.logindjango.contrib.auth.views.LoginView

django.contrib.auth.views.logoutdjango.contrib.auth.views.LogoutView

这篇关于AttributeError:模块Django.contrib.auth.views没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆