Permalink
Browse files

Break out ajax into a different view/url

  • Loading branch information...
1 parent d5a1c7b commit a31b58b6813cc292bb27aeeced839734604391cc @paltman paltman committed Jan 2, 2013
Showing with 40 additions and 20 deletions.
  1. +5 −0 waitinglist/forms.py
  2. +10 −0 waitinglist/templates/waitinglist/_list_signup.html
  3. +1 −0 waitinglist/urls.py
  4. +24 −20 waitinglist/views.py
View
@@ -22,6 +22,11 @@ def clean_email(self):
"date": entry.created.strftime("%m/%d/%y"),
}
)
+
+ def __init__(self, *args, **kwargs):
+ super(WaitingListEntryForm, self).__init__(*args, **kwargs)
+ self.fields["email"].widget.attrs["placeholder"] = "your@email.com"
+ self.fields["email"].label = ""
class CohortCreate(forms.ModelForm):
@@ -0,0 +1,10 @@
+{% load bootstrap_tags %}
+<div id="waitinglist">
+ <p>Enter your email address to reserve your spot in our private beta&hellip;</p>
+
+ <form method="POST" action="{% url waitinglist_ajax_list_signup %}" class="form-inline ajax" data-replace="#waitinglist">
+ {% csrf_token %}
+ {{ form|as_bootstrap }}
+ <input type="submit" class="btn" value="Submit" />
+ </form>
+</div>
View
@@ -4,6 +4,7 @@
urlpatterns = patterns("",
url(r"^list_signup/$", "waitinglist.views.list_signup", name="waitinglist_list_signup"),
+ url(r"^ajax_list_signup/$", "waitinglist.views.ajax_list_signup", name="waitinglist_ajax_list_signup"),
url(r"^success/$", direct_to_template, {"template": "waitinglist/success.html"}, name="waitinglist_success"),
url(r"^cohorts/$", "waitinglist.views.cohort_list", name="waitinglist_cohort_list"),
url(r"^cohorts/create/$", "waitinglist.views.cohort_create", name="waitinglist_cohort_create"),
View
@@ -5,6 +5,7 @@
from django.shortcuts import render, redirect, get_object_or_404
from django.template import RequestContext
from django.template.loader import render_to_string
+from django.views.decorators.http import require_POST
from django.contrib.auth.models import User
@@ -13,31 +14,34 @@
from waitinglist.models import WaitingListEntry, Cohort, SignupCodeCohort
+@require_POST
+def ajax_list_signup(request):
+ form = WaitingListEntryForm(request.POST)
+ if form.is_valid():
+ form.save()
+ data = {
+ "html": render_to_string("waitinglist/_success.html", {
+ }, context_instance=RequestContext(request))
+ }
+ else:
+ data = {
+ "html": render_to_string("waitinglist/_list_signup.html", {
+ "form": form,
+ }, context_instance=RequestContext(request))
+ }
+ return HttpResponse(json.dumps(data), mimetype="application/json")
+
+
def list_signup(request, post_save_redirect=None):
if request.method == "POST":
form = WaitingListEntryForm(request.POST)
if form.is_valid():
form.save()
- if request.is_ajax():
- data = {
- "html": render_to_string("waitinglist/_success.html", {
- }, context_instance=RequestContext(request))
- }
- return HttpResponse(json.dumps(data), mimetype="application/json")
- else:
- if post_save_redirect is None:
- post_save_redirect = reverse("waitinglist_success")
- if not post_save_redirect.startswith("/"):
- post_save_redirect = reverse(post_save_redirect)
- return redirect(post_save_redirect)
- elif request.is_ajax():
- data = {
- "html": render_to_string("waitinglist/_list_signup.html", {
- "form": form,
- }, context_instance=RequestContext(request))
- }
- return HttpResponse(json.dumps(data), mimetype="application/json")
-
+ if post_save_redirect is None:
+ post_save_redirect = reverse("waitinglist_success")
+ if not post_save_redirect.startswith("/"):
+ post_save_redirect = reverse(post_save_redirect)
+ return redirect(post_save_redirect)
else:
form = WaitingListEntryForm()
ctx = {

0 comments on commit a31b58b

Please sign in to comment.