I am getting an attribute error while trying to build a user registration form using Django

Viewed 28

So I have been trying to create an authentication form using Django but I am getting an attribute error which I'm not able to resolve. The code is given below.

models.py file:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class UserProfileInfo(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)

    # additional
    portfolio_site = models.URLField(blank=True)

    profile_pic = models.ImageField(upload_to='profile_pics',blank=True)

    def __str__(self):
        return self.user.username

forms.py file:

from django import forms
from django.contrib.auth.models import User
from basic_app.models import UserProfileInfo

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta():
        model = User
        fields = ('username', 'email', 'password')

class UserProfileInfoForm(forms.ModelForm):
    class Meta():
        model = UserProfileInfo
        fields = ('portfolio_site', 'profile_pic')

app urls.py file:

from django.contrib import admin
from django.conf.urls import url
from django.urls import path, include
from . import views

app_name = 'basic_app'

urlpatterns = [
    path('', views.index, name='index'),
    path('register/', views.register, name='register'),

]

views.py file:

from django.shortcuts import render
from basic_app.forms import UserForm, UserProfileInfoForm
from . import forms
# Create your views here.
def index(request):
    return render(request, 'basic_app/index.html')

def reqister(request):
    registered = False

    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password) #this one
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()

            registered = True

        else:
            print(user_form.errors, profile_form.errors)

    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()

    return render(request, 'basic_app/registration.html',
                            {'user_form':user_form,
                            'profile_form': profile_form,
                            'registered': registered})

The error message:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\management\base.py", line 366, in execute
    self.check()
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\management\base.py", line 392, in check
    all_issues = self._run_checks(
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\management\commands\migrate.py", line 64, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\management\base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core
\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\urls
\resolvers.py", line 407, in check
    for pattern in self.url_patterns:
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\util
s\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\urls
\resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

  File "C:\Users\divya\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\util
s\functional.py", line 48, in __get__
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\divya\OneDrive\Desktop\4th sem books\Stack dev\backend\Django\django_lvl_five\learning_users\basic_app\urls.py", line 10, in <module>
    path('basic_app/register/', views.register, name='register'),
AttributeError: module 'basic_app.views' has no attribute 'register'

I have been trying to successfully make migrations but I am constantly getting the aforementioned error. Please help me out with this. Thanks.

2 Answers

In Views.py


...
...
...


def register(request): # def reqister(request): 
   registered = False

...
...
...

you have to work on spelling you put 'reqister' in the views.py instead of 'register'

Related