Answer the question
In order to leave comments, you need to log in
Why is the data from the form not saved to the database?
I'm doing registration on django, but for some reason I can't save everything to the database. Tried to save everything in two ways.
First, in forms.py I created the form like this:
class RegistationForm(UserCreationForm):
username = CharField(widget=TextInput(attrs={'class': 'form-control',
'placeholder': 'Имя пользователя'}))
email = CharField(widget=TextInput(attrs={'class': 'form-control',
'placeholder': 'Логин'}))
password1 = CharField(widget=TextInput(attrs={'class': 'form-control',
'placeholder': 'Пароль'}))
class Meta:
model = User
fields = ["username", "email", "password1"]
widgets = {
"username": TextInput(attrs={'class': 'form-control',
'placeholder': 'Имя пользователя'}),
"email": TextInput(attrs={'class': 'form-control',
'placeholder': 'Логин'}),
"password1": TextInput(attrs={'class': 'form-control',
'placeholder': 'Пароль'}),
}
<form method="post">
{% csrf_token %}
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-user"></i> </span>
</div>
{{ form.username }}
</div>
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-envelope"></i> </span>
</div>
{{ form.email }}
</div>
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-lock"></i> </span>
</div>
{{ form.password1 }}
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block"> Создать </button>
</div>
<p class="text-center">Уже есть аккаунт? <a href="">Войти</a> </p>
</form>
def registation(request):
if request.method == 'POST':
form = RegistationForm(request.POST)
if form.is_valid():
form.save()
else:
error = ''
context = {'form': form
}
template = 'registation.html'
return render(request, template, context)
class RegisterFormView(FormView):
form_class = RegistationForm
template_name = 'registation.html'
success_url = reverse_lazy('home')
def form_valid(self, form):
form.save()
return super(RegisterFormView, self).form_valid(form)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question