Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # forms.py
- from django import forms
- from django.contrib.auth.forms import UserCreationForm
- from .models import User, Client, Company, Job
- class UserForm(UserCreationForm):
- class Meta:
- model = User
- fields = ("email",)
- class ClientForm(forms.ModelForm):
- class Meta:
- model = Client
- fields = ("first_name", "last_name", "birth", "skills")
- # views.py
- from django.shortcuts import render, redirect
- from django.contrib.auth import login, authenticate
- from django.views import generic
- from .forms import UserForm, ClientForm
- class ClientFormView(generic.View):
- def get(self, request, *args, **kwargs):
- template_name = "users/registration/client_form.html"
- context = {"form_user": UserForm, "form_client": ClientForm}
- return render(request, template_name, context)
- def post(self, request, *args, **kwargs):
- template_name = "users/registration/client_form.html"
- context = {"form_user": UserForm, "form_client": ClientForm}
- form_user = UserForm(request.POST)
- form_client = ClientForm(request.POST)
- if form_user.is_valid() and form_client.is_valid():
- email = form_user.cleaned_data["email"]
- password_raw = form_user.cleaned_data["password1"]
- form_user.save()
- instance_client = form_client.save(commit=False)
- user = authenticate(email=email, password=password_raw)
- if user is not None:
- instance_client.user = user
- instance_client.save()
- login(request, user)
- return redirect('main:home')
- return render(request, template_name, context)
- # client_form.html
- <form action="." method="post">
- {% csrf_token %}
- {{form_user.as_p}}
- {{form_client.as_p}}
- <button type="submit">SEND</button>
- </form>
- # models.py
- from django.db import models
- from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
- from .managers import UserManager
- import tagulous
- # Create your models here.
- class User(AbstractBaseUser, PermissionsMixin):
- email = models.EmailField(unique=True, max_length=250)
- date_joined = models.DateTimeField(auto_now_add=True)
- is_staff = models.BooleanField(default=False)
- is_active = models.BooleanField(default=True)
- objects = UserManager()
- USERNAME_FIELD = "email"
- REQUIRED_FIELDS = []
- class Meta:
- ordering = ['email']
- # for clients
- class Client(models.Model):
- user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="client")
- first_name = models.CharField(max_length=30)
- last_name = models.CharField(max_length=60)
- birth = models.DateField()
- skills = tagulous.models.TagField(force_lowercase=True)
- def get_full_name(self):
- return f"{self.first_name} {self.last_name}"
- def get_short_name(self):
- return self.first_name
- def __str__(self):
- return f"{self.first_name} - {self.user.email}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement