how can i create a correct sign-in And sign-up with custom user model(OneToOneField) in django ? im using normal HTML form . im using Django 2.2.3 . my database is Postgeres.
hi, i created a website that sign in and sign up where ok , But ... i needed custom user model .. so i created a custom user model (using one on one field ) , but i don't know how to create sign-in and sign-up for it... so i tryed ... but it won't work correctly... i can't make a sign up with additional fields in my custom user model , it sign up with only User , not Profile and gives me this error "'Manager' object has no attribute 'create_profile'". and for my sign-in .. i think its sign-in with Django's User. im using Django 2.2.3 . my database is Postgeres. how can i create a correct sign-in And sign-up ?
my models.py:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
address = models.TextField(max_length=500)
phone = models.CharField(max_length=50)
postcode = models.CharField(max_length=50, blank=True)
isManager = models.BooleanField(default=False)
isAccountment = models.BooleanField(default=False)
isStorekeeper = models.BooleanField(default=False)
isSeller = models.BooleanField(default=False)
isNormal = models.BooleanField(default=True)
def __str__(self):
return self.user.email
def create_profile(sender,**kwargs):
if kwargs['created']:
user_profile=Profile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile,sender=User)
my views.py :
from django.shortcuts import render , redirect
from django.contrib.auth.models import User
from accounts.models import Profile
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.core.mail import send_mail
def signup(request):
if request.method == 'POST':
#USER HAS INFO AND WANTS ACCOUNT___NOW!
if request.POST['password1'] == request.POST['password2']:
try:
user = User.objects.get(username=request.POST['username'],)
return render(request,'accounts/signup.html',{'error':'Username Exist...Try Something Else !','username':user})
except User.DoesNotExist :
user = User.objects.create_user(request.POST['username'] , password = request.POST['password1'],first_name=request.POST['fname'],last_name=request.POST['lname'],email=request.POST['email'],)
profile = user.profile # because of signal and one2one relation
profile.phone = request.POST['mobile_num']
profile.address = request.POST['address']
profile.postcode = request.POST['post_code']
profile.save()
auth.login(request,user)
return redirect('homepage')
return render(request,'accounts/signup.html',{'error':"Passwords Don't Match"})
else:
#USER wants enter info
return render(request,'accounts/signup.html')
def login(request):
if request.method == 'POST':
user=auth.authenticate(username=request.POST['username'],password=request.POST['password'])
if user is not None:
auth.login(request,user)
return render(request,'products/home.html')
#return redirect('homepage')
else:
return render(request,'accounts/login.html',{'error':'Username Or Password Is Incorrect !'})
else:
return render(request,'accounts/login.html')
def logout(request):
if request.method =='POST':
auth.logout(request)
return redirect('homepage')
@login_required
def myproducts(request):
user_id=request.user.id
products=Products.objects.all().filter(creator__exact=user_id)
return render(request,'accounts/myproducts.html',{'products':products})
def about_us(request):
return render(request,'accounts/aboutus.html')
def connect_us(request):
return render(request,'accounts/connectus.html')
and this is my form page :
{% extends 'base.html' %}
{% block content%}
<h1 style="margin-left:490px"> Register </h1>
<div class="left" style="margin-left:465px">
<form method="POST" action="{% url 'signup' %}">
{% csrf_token %}
First Name:
<br/>
<input type="text" name="fname"/><br/>
Last Name:
<br/>
<input type="text" name="lname"/><br/>
UserName:
<br/>
<input type="text" name="username"/><br/>
Mobile:
<br/>
<input type="text" name="mobile_num"/><br/>
Email:
<br/>
<input type="text" name="email"/><br/>
Address:
<br/>
<input type="text" name="address"/><br/>
Post Code
<br/>
<input type="text" name="post_code"/><br/>
Password:
<br/>
<input type="password" name="password1"/><br/>
Confirm password:
<br/>
<input type="password" name="password2"/>
<br/>
<br/>
<input class="btn btn-primary" type="submit" name="signup-btn" value="SignUp" style="margin-top:-23px;margin-left:49px;"/>
</form>
{% if error %}
<p style="color:red">{{error}}</p>
<br/>
{% endif%}
</div>
{% endblock %}
please help .