Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed newsletter subscription form

  • Loading branch information...
commit 548fc5136b24a8bc210482a087b345ce90c33efa 1 parent cfb695d
@denibertovic denibertovic authored
View
20 kset/staticfiles/frontend/js/custom-functions.js
@@ -63,10 +63,14 @@ $( function()
"/subscribe/",
{ email: $('#newsletter-subscription').val()},
function (data) {
- $('#newsletter-message').html(data);
- $('#newsletter-message').show();
+ $('#newsletter-subscription').val(''); // clear form
+ $('#newsletter-message').html(data);
+ $('#newsletter-message').show();
}
- );
+ ).fail(function(data) {
+ $('#newsletter-message').html(data.responseText);
+ $('#newsletter-message').show();
+ });
});
// use enter key as well
$('#form-newsletter').submit( function (event){
@@ -74,10 +78,14 @@ $( function()
"/subscribe/",
{ email: $('#newsletter-subscription').val()},
function (data) {
- $('#newsletter-message').html(data);
- $('#newsletter-message').show();
+ $('#newsletter-subscription').val(''); // clear form
+ $('#newsletter-message').html(data);
+ $('#newsletter-message').show();
}
- );
+ ).fail(function(data) {
+ $('#newsletter-message').html(data.responseText);
+ $('#newsletter-message').show();
+ });
event.preventDefault();
});
View
2  kset/templates/newsletter-form.html
@@ -2,6 +2,8 @@
<h1>Newsletter</h1>
<form id="form-newsletter">
+ {% csrf_token %}
+
<input type="text" name="email" id="newsletter-subscription" class="newsletter-subscription" value="vaša e-mail adresa" />
<input type="button" id="newsletter-submit" value="OK" />
<!-- TODO: CAPTCHA -->
View
2  newsletter/forms.py
@@ -10,6 +10,8 @@ class Meta:
model = Subscription
def clean_email(self):
+ if not self.cleaned_data.get('email'):
+ raise forms.ValidationError('Molimo unesite email adresu.')
if Subscription.objects.filter(email=self.cleaned_data.get('email')):
raise forms.ValidationError('Email adresa je već pretplaćena.')
return self.cleaned_data.get('email')
View
6 newsletter/views.py
@@ -2,17 +2,17 @@
from django.http import HttpResponse
from django.views.decorators.http import require_POST
-from django.views.decorators.csrf import csrf_exempt
from .forms import SubscriptionForm
@require_POST
-@csrf_exempt
def subscribe(request):
form = SubscriptionForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponse('Uspješno ste se pretplatili!')
else:
- return HttpResponse(form.errors.values(), status=400)
+ return HttpResponse(
+ form.errors.get('email', 'Oops! Something went wrong.'),
+ status=400)
Please sign in to comment.
Something went wrong with that request. Please try again.