AttributeError: module Django.contrib.auth.views has no attribute

AttributeError: module Django.contrib.auth.views has no attribute



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.



Here is my code:



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

]






Please post your full error traceback

– rollinger
Nov 2 '17 at 0:33






Turns out that I messed up the url.py part for registration. Thanks for answering though!

– TheCoxer
Nov 2 '17 at 0:38






Always write the Django Version as there is a lot of development going on in the versions.

– Timo
Sep 8 '18 at 10:00




9 Answers
9



Open urls.py and replace:


urls.py



django.contrib.auth.views.login with django.contrib.auth.views.LoginView


django.contrib.auth.views.login


django.contrib.auth.views.LoginView



django.contrib.auth.views.logout with django.contrib.auth.views.LogoutView


django.contrib.auth.views.logout


django.contrib.auth.views.LogoutView



Your urlpatterns should be:


from django.contrib.auth import views as auth_views

urlpatterns = [
url( r'^login/$',auth_views.LoginView.as_view(template_name="useraccounts/login.html"), name="login"),
]



In django version 2.1 in custom urls patterns from auth app i use


from django.urls import path, re_path
from django.contrib.auth import views as auth_views
from django.conf import settings
from .views import register_view, activate


urlpatterns = [
# url(r'^$', HomeView.as_view(), name='home'),
re_path(r'^register/$', register_view, name='signup'),
re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_-]+)/(?P<token>[0-9A-Za-z]1,13-[0-9A-Za-z]1,20)/$',
activate, name='users_activate'),
re_path('login/', auth_views.LoginView,
'template_name': "users/registration/login.html",
name='login'),
re_path('logout/', auth_views.LogoutView,
'next_page': settings.LOGIN_REDIRECT_URL, name='logout'),

re_path(r'^password_reset/$', auth_views.PasswordResetView,
'template_name': "users/registration/password_reset_form.html",
name='password_reset'),
re_path(r'^password_reset/done/$', auth_views.PasswordResetDoneView,
'template_name': "users/registration/password_reset_done.html",
name='password_reset_done'),
re_path(r'^reset/(?P<uidb64>[0-9A-Za-z_-]+)/(?P<token>[0-9A-Za-z]1,13-[0-9A-Za-z]1,20)/$',
auth_views.PasswordResetConfirmView,
'template_name': "users/registration/password_reset_confirm.html",
name='password_reset_confirm'),
re_path(r'^reset/done/$', auth_views.PasswordResetCompleteView,
'template_name': "users/registration/password_reset_complete.html",
name='password_reset_complete'),
]



it should be:


url(r'^registration/$', views.registration, 'template_name': 'useraccounts/registration.html', name='registration'),



auth_views does not have registration, your views does






Thanks. But now when I open useraccounts/registration.html, I get the error: "The view useraccounts.views.registration didn't return an HttpResponse object. It returned None instead." Also, did I do my forms and views correctly?

– TheCoxer
Nov 2 '17 at 0:37






@Roman Kozinets already answered it

– realmbit
Nov 2 '17 at 21:46



You need LoginView, etc. as a class and not a function as seen here (new in Django 1.11 as Should and a Must from Version 2.1 on)


LoginView


Should



I can't leave comments so I decided to leave an answer.
You have extra indent near else block. Your registration func should look like:


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)



This is why you get this error



The view useraccounts.views.registration didn't return an HttpResponse object. It returned None instead.



Open urls.py and replace:



change views.login => views.LoginView.as_view()



Django 2.1 contrib views change from function view to class view and name also change so in forgot process you need to give other view name



urls.py


from django.contrib.auth import views as auth_views

path('password_reset/', auth_views.PasswordResetView.as_view(), 'template_name':'registration/Reset_email.html', name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), 'template_name':'registration/Reset_Email_Sent.html', name='password_reset_done'),
re_path('reset/(?P<uidb64>[0-9A-Za-z_-]+)/(?P<token>[0-9A-Za-z]1,13-[0-9A-Za-z]1,20)/', auth_views.PasswordResetConfirmView.as_view(), 'template_name' : 'registration/Forgot_password.html', name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), 'template_name' : 'registration/Signin.html', name='password_reset_complete'),



Django you can customize user model and you can remove username and use email address



models.py



user model you can write customize column you can add and you can delete



user manage also you can customize comond like super user if yo
u need to give any default value


from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser,BaseUserManager
from django.utils.translation import ugettext_lazy as _

class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""

use_in_migrations = True

def _create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)

if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')

return self._create_user(email, password, **extra_fields)


class User(AbstractUser):

username = None
email = models.EmailField(_('email'), unique=True)
first_name = models.CharField( _('first name'), max_length=250)
last_name = models.CharField(_('last name'), max_length=250)
email_confirmed = models.BooleanField(default=False)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name',]

objects = UserManager()

def __str__(self):
return "%s" %(self.email)



settings.py



settings you need to give your custom user model


# AUTH USER MODEL
AUTH_USER_MODEL = "Accounts.User"

LOGIN_URL = '/login/'
#LOGIN_REDIRECT_URL = 'login_success'

LOGOUT_REDIRECT_URL = '/login/'



admin.py



admin you need to register user model


## user model view
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _

@admin.register(User)
class UserAdmin(DjangoUserAdmin):
"""Define admin model for custom User model with no email field."""

fieldsets = (
(None, 'fields': ('email', 'password')),
(_('Personal info'), 'fields': ('first_name', 'last_name', 'email_confirmed')),
(_('Permissions'), 'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')),
(_('Important dates'), 'fields': ('last_login', 'date_joined')),
)

add_fieldsets = (
(None,
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'first_name', 'last_name'),
),
)
list_display = ('email', 'first_name', 'last_name',)
search_fields = ('email', 'first_name', 'last_name')
ordering = ('-id',)



Very Simple Steps:



Goto the Projects urls.py



change "views.login => views.LoginView.as_view()"



if you are using Logout atribute then do the same with that



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế