Unable to save data from both forms - python

I am trying to get Seller information, username, password, name, mobile number and address. I have used User to get the username and password, connected the username to my model through OneToOneField relationship and later save the information.
I have made a model named SellerDetails which gets the user field from OneToOneField with User and rest all details are provided as follows:
#models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class SellerDetails(models.Model):
"""docstring for SellerDetails."""
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=128, null = True)
address = models.CharField(max_length=256)
mobile_number = models.IntegerField(primary_key=True)
def __str__(self):
return self.user.username
then I have made two forms in my forms.py
#forms.py
from django import forms
from django.contrib.auth.models import User
from avkara.models import SellerDetails, VendorDetails
class UserForm(forms.ModelForm):
password = forms.CharField(widget = forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'password')
class SellerDetailsForm(forms.ModelForm):
"""docstring for SellerDetailsForm."""
class Meta:
model = SellerDetails
fields = ('name','address','mobile_number')
#Then I tried established a relation while saving the form
#views.py
def Signup_seller(request):
seller_registered = False
if request.method == "POST":
user_form = UserForm(data = request.POST)
user_details = SellerDetailsForm(data = request.POST)
if user_form.is_valid() and user_details.is_valid():
seller = user_form.save()
seller.set_password(seller.password)
seller.save()
#saving user_details now
other_details = user_details.save(commit= False)
other_details.user = seller
other_details.save()
seller_registered = True
else:
user_form = UserForm()
user_details = SellerDetailsForm()
return render(request, 'avkara/signup_seller.html',{'seller_registered': seller_registered , 'user_form' : user_form, 'user_details' : user_details})
#Then I tried serving both forms through Html form. Here is my html
#signup_seller.html
<div class="container jumbotron">
<h2>Sign up</h2><br><br><br>
<form class="form-horizontal" method="POST">
{% csrf_token %}
{{ user_form.as_p }}
{{ user_details.as_p }}
<input type="submit" value="Submit">
</form>
</div>
#The desired out should be Thank you for registering but I get following error
IntegrityError at /avkara/signup_seller
NOT NULL constraint failed: avkara_sellerdetails.password
#This is error it is showing
F:\Webpage\python\techforstock\avkara\views.py in Signup_seller
other_details.save() <!--error part --!>
I have worked one it for hours, but no luck, Can Anyone help me out with this.
I want the username and password to be registered in users section and other information saved in Sellerdetails when I open Admin interface, but it should be with respect to the specific username.

Related

How to automatically link the users table to another table once a user gets registered in django

I am trying to create an e-commerce website, in which I have used the functionality of the Django authentication to allow users to register and login. However, in the registration form, I take the information from 2 tables at once, like this...
In my models.py...
class Profile (models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
mobileNumber = models.CharField(max_length=13)
address = models.ForeignKey(Shipping, on_delete=models.DO_NOTHING, default=False)
guest = models.BooleanField(default=False)
class Shipping(models.Model):
addressLine1 = models.CharField(max_length=100)
addressLine2 = models.CharField(max_length=100)
city = models.CharField(max_length=40)
postalCode = models.CharField(max_length=5)
landmark = models.CharField(blank=True, max_length=80)
And my forms.py...
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
from store.models import Shipping
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
first_name = forms.CharField()
last_name = forms.CharField()
mobile_number = forms.CharField()
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'mobile_number', 'email', 'password1', 'password2']
class AddressRegisterForm(forms.ModelForm):
class Meta:
model = Shipping
fields = ['addressLine1', 'addressLine2', 'city', 'postalCode', 'landmark']
My signals.py...
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile
#receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
My views.py...
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm
from .forms import AddressRegisterForm
from .models import Profile
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from . import signals
import django.dispatch
# Create your views here.
def register(request):
if not request.user.username :
if request.method == 'POST':
form = UserRegisterForm(request.POST)
form1 = AddressRegisterForm(request.POST)
if form.is_valid() and form1.is_valid():
email = form.cleaned_data.get('email')
if not User.objects.filter(email=email).exists():
username = form.cleaned_data.get('username')
messages.success(request, f"The Account created for {username}! You are now able to login.")
form1.save()
form.save()
return redirect('login')
else:
messages.warning(request, f"The email is either invalid or already used to create an account")
else:
form1 = AddressRegisterForm()
form = UserRegisterForm()
return render (request, 'register.html', {"form":form, "form1":form1})
else:
username = request.user.username
messages.warning(request, f"You are already logged in as {username}!")
return redirect ('home')
And finally my template...
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load static %}
{% block title %}Register{% endblock %}
{% block content %}
<div class="content-section mb-5">
<form method="POST">{% csrf_token %}
<div style="display: flex; width: 100%; justify-content: space-around;">
<fieldset class="form-group mt-4" style="width: 48%;">
<legend class="border-bottom mb-4">Register</legend>
{{ form | crispy }}
</fieldset>
<fieldset class="form-group mt-3" style="width: 48%;">
<legend class="border-bottom mb-4">Shipping info</legend>
{{ form1 | crispy }}
</fieldset>
</div>
<div class="form-group mt-3">
<button class="btn btn-outline-info" type="submit">Sign up</button>
<small class="text-muted ml-3">Forgot Password?</small>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">Already have an account? Sign in</small>
</div>
</div>
{% endblock %}
My information gets stored in the relevant tables, however, there seems to be no effect on the Profile table and remains to be empty...
Is there any way I can fire off django to link the user and the address in the Profile table once the user is registered.
Any help would be appreciated,
Thanks a lot!!
You need to load the signals in the ready() function of your app configuration:
Where should this code live?
Strictly speaking, signal handling and registration code can live anywhere you like, although it’s recommended to avoid the application’s root module and its models module to minimize side-effects of importing code.
In practice, signal handlers are usually defined in a signals submodule of the application they relate to. Signal receivers are connected in the ready() method of your application configuration class. If you’re using the receiver() decorator, import the signals submodule inside ready().
yourapp/apps.py:
class YourAppConfig(AppConfig):
name = "yourapp"
def ready(self):
from . import signals
That should be enough to get the signals registered at the correct time.
Regarding two issues:
Existing user models in your database won't get updated, only new ones. Either drop your db and create new users or add a data migration if your database is live or you want to keep the current.
Models connected by signals must be created without having any data for them. So you either must have nullable fields or fields with defaults.
Two forms
So you've posted 2 forms as one form in the HTML, this works because your forms don't share field names (except primary key, which isn't posted). For future reference, you can use a form prefix to deal with duplicate field names and even use the same form for different purposes (shipping and billing address for example).
On signals
Signals are used when an event happens (user is saved, user logs in), that is part of 3rd party code (in this case Django). But when you're the one creating the event, they are not needed as you can just do whatever you want right at where you create the event.
Logged in or not
The correct test is:
if not request.user.is_authenticated:
On to the solution
When saving related models with one or more forms, you should first create the models that can be saved without the other model of the relationship existing. These are:
The model a foreign key points to (Shipping).
The model of a OneToOneField, where the OneToOneField is not defined (User)
Due to the signal, our profile model will be created with empty values, but the form does not know that the mobile_number belongs on the Profile model, because it is a ModelForm bound to the User Model. It treats mobile_number as a field that you will handle yourself. So let's do that:
user = form.save() # save returns the model instance created
profile = user.profile
profile.mobile_number = form.cleaned_data['mobile_number']
As said, we can create the address without problems and then we can link it to the profile:
address = form1.save() # save returns the model instance created
profile.address = address
And now we can save the profile:
profile.save()
So putting it all together:
def register(request):
if not request.user.is_authenticated: # The correct way to test for logged in user
if request.method == "POST":
form = UserRegisterForm(request.POST)
form1 = AddressRegisterForm(request.POST)
if form.is_valid() and form1.is_valid():
email = form.cleaned_data.get("email")
if not User.objects.filter(email=email).exists():
username = form.cleaned_data.get("username")
messages.success(
request,
f"The Account created for {username}! You are now able to login.",
)
user = form.save()
profile = user.profile
profile.mobile_number = form.cleaned_data['mobile_number']
address = form1.save()
profile.address = address
profile.save()
return redirect("login")
else:
messages.warning(
request,
f"The email is either invalid or already used to create an account",
)
else:
form1 = AddressRegisterForm()
form = UserRegisterForm()
return render(request, "register.html", {"form": form, "form1": form1})
else:
username = request.user.username
messages.warning(request, f"You are already logged in as {username}!")
return redirect("home")
You can add all the fields related to the shipping address in the "profile" model.
When you want to access that data, you can simply use querysets.
In the models
class Profile (models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
mobileNumber = models.CharField(max_length=13)
address = models.ForeignKey(Shipping, on_delete=models.DO_NOTHING, default=False)
guest = models.BooleanField(default=False)
addressLine1 = models.CharField(max_length=100)
addressLine2 = models.CharField(max_length=100)
city = models.CharField(max_length=40)
postalCode = models.CharField(max_length=5)
landmark = models.CharField(blank=True, max_length=80)
and when you want to access the data, you can simply use querysets

Django form: phone field not showing

I would like to create a contact form on my Django website.
For now, this is my code:
models.py:
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
class Client(models.Model):
phone = PhoneNumberField(null=False, blank=True, unique=True)
forms.py:
from django import forms
from phonenumber_field.modelfields import PhoneNumberField
class ContactForm(forms.Form):
fullName = forms.CharField(max_length=100)
email = forms.EmailField()
phone = PhoneNumberField()
message = forms.CharField(widget=forms.Textarea)
views.py:
def contact(request):
# return render(request, 'contact.html')
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# send email code goes here
return HttpResponse('Thanks for contacting us!')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
html:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
I of course installed phonenumber_field and added it in settings.py
This is the result, phone field missing:
Any help is hugely appreciated! Thanks for your time.
You used a model field, whereas for a form, you need a form field:
from django import forms
# a form field &downarrow;
from phonenumber_field.formfields import PhoneNumberField
class ContactForm(forms.Form):
fullName = forms.CharField(max_length=100)
email = forms.EmailField()
phone = PhoneNumberField()
message = forms.CharField(widget=forms.Textarea)

Not getting model form values in template

Django user profile model form data is not getting displayed on the template, not even giving me an error!
I am learning to create Django registration form with user profile models I have created the registration form and profile form successfully but I am not getting any values from models.py file.
Models.py
class ExtenduserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birth_date = models.DateField(null=True, blank=True)
age = models.IntegerField()
def __str__(self):
return self.user.username
#receiver(post_save,sender=User)
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile =
ExtenduserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=User)
forms.py
class userprofile(forms.ModelForm):
birth_date = forms.DateField(help_text='Required. Format: YYYY-MM-DD')
class Meta:
model = ExtenduserProfile
fields = ('age','birth_date')
views.py
#login_required()
def UserProfile(request,pk=None):
profile = ExtenduserProfile.objects.all()
return render(request,'dashboard/profile.html',{'profile':profile})
#login_required()
def HomeScreen(request):
if request.user.is_authenticated:
username = request.user.username
else :
username = 'not logged in'
context = {'username':username,'user':user}
return render(request,'dashboard/Home.html',context)
def singup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
user_profile = userprofile(request.POST)
if form.is_valid() and user_profile.is_valid():
user = form.save()
profile = user_profile.save(commit=False)
profile.user = user
profile.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username = username, password = password)
login(request,user)
return redirect(login_view)
else:
form = SignupForm()
user_profile = userprofile()
context = {'form':form, 'user_profile':user_profile }
return render(request,'account/signup.html',context)
HTML file
{% extends 'dashboard/base.html' %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Profile</title>
</head>
{% block content%}
<body>
<h3>Welcome to profile page</h3><br>
{% if user.is_authenticated %}
<h4>Name = {{user.first_name}} {{user.last_name}}</h4>
<h4>Email = {{user.email}}</h4>
<h4>Age = {{user.ExtenduserProfile.age}} </h4>
<h4>DOB = {{user.ExtenduserProfile.birth_date}} </h4>
{% endif %}
</body>
{% endblock%}
</html>
my expected output should be
name: xyz
email: abc#abc.com
age: 24
DOB: 1994-04-21
What is a Custom User Model Extending AbstractUser?
It is a new User model that inherit from AbstractUser. It requires a special care and to update some references through the settings.py. Ideally it should be done in the begining of the project, since it will dramatically impact the database schema. Extra care while implementing it.
When should I use a Custom User Model Extending AbstractUser?
You should use it when you are perfectly happy with how Django handles the authentication process and you wouldn’t change anything on it. Yet, you want to add some extra information directly in the User model, without having to create an extra class.
I suggest to you to use Abstract User
Which is easier and best practice for your case
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)
Also add this to your setting :
AUTH_USER_MODEL = 'appname.User'
Check this tutorial for more information
Well, when you are using reverse relation in OneToOne, the class name should be in all lowercase(documentation). For example:
<h4>Age = {{user.extenduserprofile.age}} </h4>
<h4>DOB = {{user.extenduserprofile.birth_date}} </h4>
Also, why are you sending profile = ExtenduserProfile.objects.all() to template via context from your profile view? Because as I can see, you are not using it. So I think you can remove that part of code as well.
Also, you can remove the {% if user.is_authenticated %} and {%endif%} from template. As, this template can't be accessible unless you are logged in. You have restricted that via login_required decorator.

I extended User Model and I don't know how to get value of it from other users through forms

I extended User Model so I made nickname.
And nickname appears on admin page but I can't get value of it from other users through forms.
The value was not saved whenever I clicked the submit button of my signup page after I filled up nickname charfield.
#models.py
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
class Profile(models.Model):
class Meta:
verbose_name = u'profile'
verbose_name_plural = u'profile'
user = models.OneToOneField(settings.AUTH_USER_MODEL)
nick = models.CharField(verbose_name=u'nickname', max_length=50, blank=True,)
I think it's working good.
#forms.py
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django import forms
from .models import Profile
class CreateUserForm(UserCreationForm):
email = forms.EmailField(required=True)
nick = forms.CharField(required=True)
class Meta:
model = User
fields = ("username", "email", "nick", "password1", "password2")
def save(self, commit=True):
user = super(CreateUserForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
user.nick = self.cleaned_data["nick"]
if commit:
user.save()
return user
But I think something is wrong with forms.py
<!--signup.html-->
{% extends 'moc/base.html' %}
{% block content %}
<form method="post" action="{% url 'signup' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">submit</button>
</form>
{% endblock %}
The nick field is on the profile, not the user. You need to create that related instance along with the user.
user = super(CreateUserForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
profile = Profile(user=user)
profile.nick = self.cleaned_data["nick"]
if commit:
user.save()
profile.save()

Django: How to update specific fields in extended model of User?

I'm relatively new to Django and am stuck on this. I've been trying to create a website where users can create their accounts, view their and other member's profile pages.
models.py
class UserProfileInfo(models.Model):
"""docstring for ClassName"""
user = models.OneToOneField(User)
status = models.TextField(blank = True)
desc = models.TextField(blank = True)
portfolio_site = models.URLField(blank = True)
profilepic = models.ImageField(blank = True, null = True, upload_to = 'profile_pics')
def __str__(self):
return self.user.username
forms.py
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 = ('desc', 'portfolio_site', 'status', 'profilepic')
So, I want the user to be able to update their status from their homepage by typing into a text box.
index.html (the homepage from where the user can update their status)
<form method="POST" action="{% url 'app5:user_status' user.username %}">
{% csrf_token %}
<div class="form-group">
<label for="inputlg"></label>
<input class="form-control input-lg" id="inputlg" type="text" name='status' placeholder="How are you feeling?">
</div><center>
<input type="submit" name='submitbtn' value='Add Status'>
</center>
</form>
Here's what my function to update the status in views.py looks like:
def user_status(request, username):
if request.method == 'POST':
mystatus = request.POST.get('status')
user = User.objects.get(username=username)
user.userprofileinfo.status = mystatus
user.save()
return redirect('app5:profilepage', username=username)
However, it does not work. If I try to update some other field like the username or email which is in the User Model instead of the UserProfileInfo extended model, user.email = mystatus works in that case. But the update doesn't take place when I try to update the status field of UserProfileInfo model. I'm pretty sure the error exists in the line: user.userprofileinfo.status= mystatus. I've searched in the documentation and many other sources but have been unable to find an answer.. How do I go about this?
Any help would be greatly appreciated. Thank you!
Edit: I do not get any error when I click on Submit. It's just that the updation doesn't take place in the model and the status field stays the same as before.
You saved the user, not the userprofileinfo object. It's a separate model, so needs to be saved separately.
profile = user.userprofileinfo
profile.status = mystatus
profile.save()
if you have update field in UserProfileInfo then get the object for UserProfileInfo and update the field as below
def user_status(request,username):
if request.method == 'POST':
mystatus = request.POST.get('status')
user = User.objects.get(username=username)
userprofileinfo = user.userprofileinfo
userprofileinfo.status = mystatus
userprofileinfo.save()
return redirect('app5:profilepage',username=username)

Categories

Resources