I'm learning Django 1.11 and I'm creating a form with widget_tweaks tools.
I have a variable "code" which is supposed to inform the template if the form is sent or not. If it's not sent, code = 1 and the form is displayed If the form is displayed, I just would like to display another form (not developed for now)
The problem is, code is always = 1
Why ?
Many Thanks
view.py
#-*- coding: utf-8 -*-
from django.shortcuts import render
from .forms import MinimumRegisterForm
# Create your views here.
def view_about(request):
return render(request, 'about.html', locals())
def view_first(request):
return render(request, 'first.html', locals())
def view_second(request):
form = MinimumRegisterForm()
if request.method == 'POST':
form = MinimumRegisterForm(request.POST)
if form.is_valid():
identifiant = form.cleaned_data['identifiant']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
confirm_password = form.cleaned_data['confirm_password']
code = "2"
else:
code = "1"
return render(request, 'second.html', locals())
return render(request, 'second.html', locals())
Template html
<div class="inner cover">
<h1 class="cover-heading">Parlons un peu de vous ...</h1>
<div class="MinimumRegisterForm">
{% if form.errors %}
Les erreurs suivantes sont survenues:
{{ form.errors }}
{% endif %}
{% if code == "1" %}
<form action="{% url 'second' %}" action="POST">
{% csrf_token %}
<p>{{ form.identifiant|add_class:"form-control"|attr:"placeholder:Quel sera votre identifiant unique ?" }}</p>
<p>{{ form.email|add_class:"form-control"|attr:"placeholder:Indiquez-y votre email !" }}</p>
<p>{{ form.password|add_class:"form-control"|attr:"placeholder:Créer votre mot de passe ici." }}</p>
<p>{{ form.confirm_password|add_class:"form-control"|attr:"placeholder:Retaper votre mot de passe." }}</p>
<input class="btn btn-lg btn-primary" type="submit" value="Continuer">
</form>
{% elif code == "2" %}
<p>Formulaire suivant</p>
{% endif %}
{{ code }}
</div>
</div>
Forms.py
from django import forms
class MinimumRegisterForm(forms.Form):
identifiant = forms.CharField(
max_length=50,
label="Choisissez un identifiant unique",
)
email = forms.EmailField(
label="Votre adresse mail",
)
password = forms.CharField(
widget=forms.PasswordInput,
label="Entrer un mot de passe",
)
confirm_password = forms.CharField(
widget=forms.PasswordInput,
label="Confirmer votre mot de passe",
)