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)
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 signup/view.py
from django.shortcuts import render
from mainpage.models import User
def signup(request):
form1=User(request.POST or None)
if form1.is_valid():
form1.save()
context = {
"form1": form1,
}
return render(request, "signup.html",context)
Traceback
Environment:
Request Method: GET
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
12. if form1.is_valid():
Exception Type: AttributeError at /signup/
Exception Value: 'User' object has no attribute 'is_valid'
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
You are calling is_valid on the User object, but that's not a method you've defined on the User (or that exists already on AbstractUser). If you take a closer look at your tutorial, they're actually calling it on the UserForm. Forms have a built-in is_valid method used to make sure all the form data submitted is valid (what valid means can differ based on your use case... maybe you're making sure everything's filled in, or that something's a real email address, or that a date is more than six months in the future).
Your code says:
form1=User(request.POST or None)
if form1.is_valid():
form1.save()
It looks like you meant to define form1 as a form object (otherwise that's maybe the worst variable name ever). You probably have something called UserForm (or similar) in your forms.py. You'll want to import that, and instantiate it like:
form1 = UserForm(request.POST, instance=request.user)
https://docs.djangoproject.com/en/2.0/ref/forms/validation/
(side note: When you come back to your code in a year, will you remember what form1 was supposed to mean? Try to name your variable something that explains itself--user_form or something similar)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With