简体   繁体   中英

'super' object has no attribute 'save' Django

I am making a registration form for users to signup using Django 1.8 and python 3.5

I have created a User(Extending User Model Using a Custom Model Extending AbstractUser)(ie I wanted to add custom fields to the default django's Users table like bio, date of birth etc)

When I try to enter username and password I get an error TypeError at /signup/

This is my mainpage/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)

This is my signup/forms.py

from django import forms
from mainpage.models import User
from django import forms
from django.contrib.auth.forms import UserCreationForm
class allusers1(forms.Form):
    username1=forms.CharField(label = "Username")
    password1=forms.CharField(label="passwordwa",max_length=40)
    class Meta:
        model=User
        fields=(
            'username',

            'password',

        )
    def save(self,commit=True):
        user=super(allusers1,self).save(commit=False)
        user.username=self.cleaned_data['username1']
        user.password=self.cleaned_data['password1']
        if commit:
            user.save()

        return user

this is signup/views.py

from django.shortcuts import render
from .forms import allusers1
def signup(request):       
    form1=allusers1(request.POST or None)
    if form1.is_valid():
        form1.save()
    context = {
   "form1": form1,
         }
    return render(request, "signup.html",context)

SOME ADDITIONAL INFO I am refering to this blog for creating AbstractUser https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#abstractuser

Traceback Environment:

Request Method: POST
Request URL: http://localhost:8000/signup/

Django Version: 1.8
Python Version: 3.5.4
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'mainpage',
 'signup',
 'login',
 'currency',
 'rest_framework',
 'corsheaders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Users\vaibhav2\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\vaibhav2\PycharmProjects\MajorProject\src\signup\views.py" in signup
  13.         form1.save()
File "C:\Users\vaibhav2\PycharmProjects\MajorProject\src\signup\forms.py" in save
  26.         user=super(allusers1,self).save(commit=False)

Exception Type: AttributeError at /signup/
Exception Value: 'super' object has no attribute 'save'

In signup/forms.py

Change:

user=super(User,self).save(commit=False)

To

user = super(allusers1, self).save(commit=False)

And read some articles on coding style. Like PEP-8 and Django style . It helps others (and your future self) to read and understand your code.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM