From 245b978ee20a7091cb2924bc14ae36f4d0460477 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sun, 22 Jan 2017 19:20:05 +0100 Subject: Utilisation d'un formulaire Django pour faire ça proprement --- forms.py | 5 +++++ templates/subscribe.html | 4 ++-- views.py | 15 ++++++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 forms.py diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..e0085d0 --- /dev/null +++ b/forms.py @@ -0,0 +1,5 @@ +from django import forms + + +class SubscribeForm(forms.Form): + email = forms.EmailField(label="Adresse email") diff --git a/templates/subscribe.html b/templates/subscribe.html index 8fe4b79..b1aa71b 100644 --- a/templates/subscribe.html +++ b/templates/subscribe.html @@ -5,9 +5,9 @@ {% block body %}
Après l'abonnement, vous allez recevoir un mail avec un lien de confirmation. Aucun autre mail ne vous sera envoyé si vous n'avez pas validé votre abonnement.
Vous pouvez vous désabonner à tout moment à l'aide d'un lien contenu dans tout les mails que nous vous enverrons.
diff --git a/views.py b/views.py
index 28b069e..c106668 100644
--- a/views.py
+++ b/views.py
@@ -3,6 +3,7 @@ from django.db.models import Count
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
+from edt.forms import SubscribeForm
from edt.models import Timetable, LastUpdate, Group, Subscription, Course
from edt.utils import get_week
@@ -46,13 +47,17 @@ def subscribe(request, timetable_slug, year, week, group_slug):
group = get_object_or_404(Group, slug=group_slug)
if request.method == "POST":
- subscription = Subscription(email=request.POST["email"], group=group)
- subscription.save()
+ form = SubscribeForm(request.POST)
+ if form.is_valid():
+ subscription = Subscription(email=request.POST["email"], group=group)
+ subscription.save()
- return redirect("timetable", timetable_slug=timetable_slug, year=year, week=week, group_slug=group_slug)
+ return redirect("timetable", timetable_slug=timetable_slug, year=year, week=week, group_slug=group_slug)
else:
- timetable = get_object_or_404(Timetable, slug=timetable_slug)
- return render(request, "subscribe.html", {"timetable": timetable, "year": year, "week": week, "group": group})
+ form = SubscribeForm()
+
+ timetable = get_object_or_404(Timetable, slug=timetable_slug)
+ return render(request, "subscribe.html", {"timetable": timetable, "year": year, "week": week, "group": group, "form": form})
def confirm_subscription(request, token):
subscription = get_object_or_404(Subscription, token=token)
--
cgit v1.2.1