Я делаю регистрационную форму на своем сайте (используйте python 3.6.5 и django 2.1.7).Я могу отображать свои формы, но когда я нажимаю кнопку ввода, ничего не происходит.Я думаю, что он не отправляет запрос в мой файл просмотраДжанго не показывает ошибок.
HTML часть.«входной» боттон не отправляет запрос в мой View.
{% extends 'index.html'%}
{% load crispy_forms_tags %}
{% block content %}
<div class='container'>
<div class='row'>
<div class='col-sm-6 col-sm-offset-3'
<form action='' method="POST">
{% csrf_token %}
{{ form|crispy }}
<input type='submit' value='Save'>
</form>
</div>
</div>
</div>
My forms.py
from django import forms
from django.contrib.auth import get_user_model
User = get_user_model()
class UserForm(forms.ModelForm):
password_check = forms.CharField(widget = forms.PasswordInput)
password = forms.CharField(widget = forms.PasswordInput)
class Meta:
model = User
fields=[
'username',
'password',
'password_check',
'first_name',
'last_name',
'email'
]
#widgets = {'username':forms.TextInput(attrs={'class':'form-control' })
#}
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args,*kwargs)
self.fields['username'].label = 'Введите Ник'
self.fields['password'].label = 'Введите Пароль'
self.fields['username'].help_text = 'Используйте буквы и цифры'
self.fields['password_check'].label = 'Подтвердите Пароля'
self.fields['first_name'].label = 'Введите Имя'
self.fields['last_name'].label = 'Введите Фамилию'
self.fields['email'].label = 'Введите Мейл'
def clean(self):
username = self.cleaned_data['username']
password = self.cleaned_data['password']
password_check = self.cleaned_data['password_check']
email = self.cleaned_data['email']
if User.objects.filter(username = username).exists():
raise forms.ValidationError('Такой ник уже существует!')
if password != password_check:
raise forms.ValidationError('Пароли не совпадают!')
И мой View.py
from mediaportalapp.models import UsersAcc
from django.contrib.auth import get_user_model
User = get_user_model()
class RegView(View):
template_name='registration.html'
def get(self, request, *args, **kwargs):
form = UserForm()
context = {'form': form}
return render(self.request, self.template_name, context)
def post(self, request, *args, **kwargs):
form = UserForm(request.POST or None)
if form.is_valid():
new_user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
new_user.set_password(password)
password_check = form.cleaned_data['password_check']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
new_user.save()
UsersAcc.objects.create(user=User.objects.get(
username=new_user.username),
first_name = new_user.first_name,
last_name = new_user.last_name,
email = new_user.email)
return HttpResponseRedirect('/')
context = { 'form' : form }
return render(self.request, self.template_name, context)
Mymodels.py
class UsersAcc(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.EmailField()
favourite_articles = models.ManyToManyField(Article)
def __str__(self):
return "{},{}".format(self.user.username, self.last_name)