Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # accounts/view.py
- from .form import SignUpForm
- from django.shortcuts import render
- from django.shortcuts import redirect
- from django.contrib.auth import login as django_login
- from django.contrib.auth import authenticate
- from django.contrib.auth.decorators import login_required
- from django.views.decorators.cache import never_cache
- from django.views.decorators.csrf import csrf_protect
- from django.views.decorators.debug import sensitive_post_parameters
- @csrf_protect
- def signup(request, template_name='accounts/signup.html'):
- if request.method == 'POST':
- form = SignUpForm(request.POST)
- if form.is_valid():
- form.save()
- username = form.cleaned_data.get('username')
- password = form.cleaned_data.get('password1')
- user = authenticate(username=username, password=password)
- django_login(request, user)
- return redirect('home')
- else:
- form = SignUpForm()
- return render(request, template_name, {'form': form})
- # accounts/form.py
- from django import forms
- from django.contrib.auth.forms import AuthenticationForm
- from django.contrib.auth.forms import UserCreationForm
- from django.contrib.auth.models import AbstractUser
- from django.contrib.auth.models import User
- class SignUpForm(UserCreationForm):
- last_name = forms.CharField(label='Фамилия', max_length=30, required=True,
- widget=forms.TextInput(attrs={'class':'form-input','type':'text','placeholder': 'Иванов'}))
- first_name = forms.CharField(label='Имя', max_length=30, required=True,
- widget=forms.TextInput(attrs={'class':'form-input','type':'text','placeholder': 'Иван'}))
- patronymic = forms.CharField(label='Отчество', max_length=30, required=True,
- widget=forms.TextInput(attrs={'class':'form-input','type':'text','placeholder': 'Иванович'}))
- email = forms.EmailField(label='Электронная почта', max_length=254,
- widget=forms.TextInput(attrs={'class':'form-input','type':'text','placeholder': 'example@example.ru'}))
- birth_day = forms.CharField(label='Дата рождения', max_length=10, required=True,
- widget=forms.DateInput(attrs={'type': 'date'}))
- inn_id = forms.CharField(label='ИНН', max_length=12, required=True,
- widget=forms.TextInput(attrs={'class':'form-input','type':'text','placeholder': 'ИНН состоит из 12 цифр'}))
- passport_s = forms.CharField(label='Серия пасспорта', max_length=4, required=True,
- widget=forms.TextInput(attrs={'class':'form-input','type':'text','placeholder': 'Серия пасспорта состоит из 4 цифр'}))
- passport_n = forms.CharField(label='Номер пасспорта', max_length=6, required=True,
- widget=forms.TextInput(attrs={'class':'form-input','type':'text','placeholder': 'Номер пасспорта состоит из 6 цифр'}))
- # accounts/model.py
- # Как сохранить нижепреведённых полей модели?
- from django.db import models
- class CustomUser(models.Model):
- user = models.OneToOneField(User, on_delete=models.CASCADE)
- email = models.EmailField(max_length=255, unique=True)
- first_name = models.CharField(max_length=50, blank=True)
- last_name = models.CharField(max_length=50, blank=True)
- patronymic = models.CharField(max_length=50, blank=True)
- birth_day = models.DateTimeField()
- inn_id = models.IntegerField(default=0, blank=True)
- passport_n = models.IntegerField(default=0, blank=True)
- passport_s = models.IntegerField(default=0, blank=True)
- # accounts/templates/accounts/signup.html
- {% extends 'form.html' %}
- {% block content %}
- <div class="container">
- <form method="post" action="{% url 'signup' %}">
- <div class="row">
- <h4>Регистрация</h4>
- {% csrf_token %}
- {% for field in form %}
- <p>
- {{ field.label_tag }}<br>
- {{ field }}
- {% for error in field.errors %}
- <p style="color: red">{{ error }}</p>
- {% endfor %}
- </p>
- {% endfor %}
- <button type="submit" class="button-submit">Регистрация <i class="fa fa-check"></i></button>
- </form>
- </div>
- {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement