Account Model:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.urls import reverse
from django.utils.text import slugify
class AccountManager(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None):
if not email:
raise ValueError('E-Mail is Required, Please Provide Your E-Mail.')
if not first_name:
raise ValueError('E-Mail is Required, Please Provide Your First Name.')
if not last_name:
raise ValueError('E-Mail is Required, Please Provide Your Last Name.')
user = self.model(email=self.normalize_email(email).lower(), first_name=first_name, last_name=last_name)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, first_name, last_name, password=None):
user = self.create_user(email=self.normalize_email(email), first_name=first_name, last_name=last_name,
password=password)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(verbose_name='email', max_length=255, unique=True)
first_name = models.CharField(verbose_name='First Name', max_length=255)
last_name = models.CharField(verbose_name='Last Name', max_length=255)
slug = models.SlugField(verbose_name='Slug', primary_key=True, unique=True, allow_unicode=True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
objects = AccountManager()
@property
def full_name(self):
return f'{self.first_name} {self.last_name}'
def __str__(self):
return f'{self.email} - {self.full_name}'
def has_perm(self, perm, obj=None):
return self.is_staff
def has_module_perms(self, app_label):
return True
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(f'{self.first_name}-{self.email}-{self.last_name}')
super(Account, self).save(*args, **kwargs)
Navbar __Partial
{% load static %}
<div class="ui grid">
<div class="column">
<div class="ui borderless top fixed stackable menu">
<a class="item" href="{% url 'Pages:HomeView' %}">
<img src="{% static 'Imgs/Anonymus.png' %}"/>
</a>
<div class="ui simple dropdown item">
About Us
<i class="dropdown icon"></i>
<div class="menu">
<a class="item" href="{% url 'Pages:HistoryView' %}">History</a>
<a class="item" href="{% url 'Pages:SafetyCertificateView' %}">Safety & Certificates</a>
</div>
</div>
<div class="ui simple dropdown item">
<div>Shop</div>
<i class="dropdown icon"></i>
<div class="menu">
<a class="item" href="#">Category Name</a>
<a class="item" href="#">Category Name</a>
<a class="item" href="#">Category Name</a>
</div>
</div>
<div class="ui simple dropdown item">
Inspiration
<i class="dropdown icon"></i>
<div class="menu">
<a class="item" href="{% url 'Pages:DevelopmentMilstoneView' %}">Child Development Milestone</a>
<a class="item" href="{% url 'Pages:RoomConceptView' %}">Room Concept</a>
<a class="item" href="{% url 'Pages:SizeGuideView' %}">Size Guide</a>
<a class="item" href="{% url 'Pages:ProjectsView' %}">Projects</a>
<a class="item" href="{% url 'Pages:TimelineView' %}">Timeline</a>
</div>
</div>
<a class="item" href="{% url 'Pages:OurStoresView' %}">Our Stores</a>
<a class="item" href="{% url 'Pages:DistributorsView' %}">Distributors</a>
<div class="ui simple dropdown item">
Support
<i class="dropdown icon"></i>
<div class="menu">
<a class="item" href="{% url 'Pages:FAQsView' %}">FAQs</a>
<a class="item" href="{% url 'Pages:TermsConditionsView' %}">Terms & Conditions</a>
</div>
</div>
<div class="ui simple dropdown item">
Contact Us
<i class="dropdown icon"></i>
<div class="menu">
<a class="item" href="{% url 'Pages:SalesOfficesView' %}">Sales Offices</a>
<a class="item" href="{% url 'Pages:WorldWideView' %}">Representative Worldwide</a>
</div>
</div>
<a class="item" href="{% url 'admin:index' %}">Admin</a>
<div class="right menu">
<form class="vertically fitted item ui form" method="POST" action="">
{% csrf_token %}
<div class="field">
<div class="ui icon input">
<input type="text" name="navbar_search_input" placeholder="Search Toys...">
<i class="circular search link icon"></i>
</div>
</div>
</form>
{% if request.user.is_authenticated %}
<div class="ui left dropdown item Account_DropDown">
<i class="user circle large icon"></i>
<div class="menu">
<div class="item">
<a href="{% url 'Accounts:ClientDetailUrl' request.user.slug %}">{{ request.user.full_name }}</a>
</div>
<div class="item">
<a href="{% url 'Accounts:ClientSignOutUrl' %}">Logout</a>
</div>
</div>
</div>
{% else %}
<div class="item">
<a href="{% url 'Accounts:ClientSignUpUrl' %}">
<i class="user icon"></i>
</a>
</div>
{% endif %}
<div class="item ui label">
<a href="#" class="ui label">
<i class="shopping cart large icon"></i> 5
</a>
</div>
</div>
</div>
</div>
</div>
Views.py: i didnt override any other methods inside this Class.
from django.views.generic import CreateView
from django.views.generic.detail import DetailView
from django.contrib.auth.views import LogoutView, LoginView
from django.urls import reverse_lazy
from Accounts.forms import ClientSignUpForm, ClientSignInForm
from Accounts.models import Account
class ClientSignUpView(CreateView):
template_name = 'Accounts/Sign_Up.html'
form_class = ClientSignUpForm
def get_success_url(self):
return reverse_lazy('Accounts:ClientSignInUrl')
def get_context_data(self, **kwargs):
context = super(ClientSignUpView, self).get_context_data(**kwargs)
context['page'] = 'Sign Up'
return context
class ClientSignInView(LoginView):
template_name = 'Accounts/Sign_In.html'
form_class = ClientSignInForm
def get_context_data(self, **kwargs):
context = super(ClientSignInView, self).get_context_data(**kwargs)
context['page'] = 'Sign In'
return context
class ClientSignOutView(LogoutView):
next_page = reverse_lazy('Accounts:ClientSignInUrl')
class ClientDetailView(DetailView):
model = Account
context_object_name = 'account'
template_name = 'Accounts/Client_Info.html'
def get_context_data(self, **kwargs):
context = super(ClientDetailView, self).get_context_data(**kwargs)
account = Account.objects.get(slug=self.kwargs['slug'])
context['account'] = account
context['page'] = account.full_name
return context