aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2017-01-22 19:20:05 +0100
committerAlban Gruin2017-01-22 19:20:05 +0100
commit245b978ee20a7091cb2924bc14ae36f4d0460477 (patch)
tree82a7275a03ea19d4dd7357d9e63a3882a3b21369
parentab3885a7cc8551061da8bc41835933a75c9937e0 (diff)
Utilisation d'un formulaire Django pour faire ça proprement
-rw-r--r--forms.py5
-rw-r--r--templates/subscribe.html4
-rw-r--r--views.py15
3 files changed, 17 insertions, 7 deletions
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 %}
<h2>S'abonner à {{ timetable.name }} &ndash; {{ group.name }}</h2>
<form action="{% url "subscribe" timetable.slug year week group.slug %}" method="post">
- <input placeholder="Votre adresse mail" type="email" name="email" />
{% csrf_token %}
- <input type="submit" />
+ {{ form }}
+ <input type="submit" value="S'abonner" />
</form>
<p>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.<br />
Vous pouvez vous désabonner à tout moment à l'aide d'un lien contenu dans tout les mails que nous vous enverrons.<br />
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)