validation - Django messages framework -
i'm looking add django messages
in form in order display success message
if form validated , error message
if form not validated (missing field, data type error, ...)
my error message works well, don't overcome display success message in next template when form valid.
this view :
def birthcertificate_form_unique_number(request) : validity = [] submission = [] #user fill fields query_social_number = request.get.get('social_number') query_social_number_father = request.get.get('social_number_father') query_social_number_mother = request.get.get('social_number_mother') success = false if request.method == 'post': form = birthcertificateform2(request.post or none) if form.is_valid() : # vérification sur la validité des données post = form.save() messages.success(request, 'le formulaire été enregistré !') return httpresponseredirect(reverse('bc_treated2', args=(messages,),kwargs={'id': post.id})) else: messages.error(request, "le formulaire est invalide !") elif request.method == 'get': form = birthcertificateform2() parent1 = person.objects.filter(social_number=query_social_number_father) parent2 = person.objects.filter(social_number=query_social_number_mother) if query_social_number : if person.objects.filter(social_number = query_social_number).exists() == true : individu = get_object_or_404(person, social_number = query_social_number) messages.success(request, 'le numéro unique existe !') form.fields['fk_parent1'].queryset = parent1 form.fields['fk_parent2'].queryset = parent2 form.fields['lastname'].initial = individu.lastname form.fields['firstname'].initial = individu.firstname form.fields['birthday'].initial = individu.birthday form.fields['birthcity'].initial = individu.birthcity form.fields['birthcountry'].initial = individu.birthcountry form.fields['sex'].initial = individu.sex form.fields['social_number'].initial = individu.social_number elif person.objects.filter(social_number = query_social_number).exists() == false : validity = false messages.error(request, "le numéro unique est invalide !") context = { "form" : form, "validity" : validity, "submission" : submission } return render(request, 'bc_form2.html', context)
and in html template :
{% message in messages %} <div class="alert {{ message.tags }} alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="close"> <span aria-hidden="true">×</span> </button> {{ message }} </div> {% endfor %}
how display success message when form valid ?
edit :
my urls.py file :
from django.conf.urls import url . import views urlpatterns = [ url(r'^bc_accueil$', views.birthcertificate_home, name="bchome"), url(r'^formulaire$', views.birthcertificate_form, name = "bcform"), url(r'^formulaire2$', views.birthcertificate_form_unique_number, name = "bcform2"), url(r'^formulaire_traite/(?p<id>\d+)/$', views.birthcertificate_resume, name="bc_treated"), url(r'^formulaire2_traite/(?p<id>\d+)/$', views.birthcertificate_resume_unique_number, name="bc_treated2"), url(r'^birthcertificate_pdf/(?p<id>\d+)/$', views.birthcertificate_pdf, name="pdf"), url(r'^not_found$', views.birthcertificate_notfound, name="bcnotfound"), ]
you need know 1 thing that, messages
execute once. why did do
args=(messages, )
you can access messages in bc_treated2
without passing args
Comments
Post a Comment