Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Django) Ajax form submit with jquery-forms
    text
    copied!<p>I'm trying to add ajax form submit to my webpage. Form will add user's email to newsletter. I've found this solution : <a href="http://www.tutorialswitch.com/web-development/quick-and-simple-ajax-forms-with-json-responses/" rel="nofollow noreferrer">http://www.tutorialswitch.com/web-development/quick-and-simple-ajax-forms-with-json-responses/</a> and now I'm trying to rewrite it for django. </p> <p>So I have my form, included on main page :</p> <pre><code> &lt;div id="form-newsletter-message"&gt;&lt;/div&gt; &lt;form action="{% url newsletter_add %}" method="post" class="form-newsletter" id="form-newsletter"&gt; &lt;input type="text" class="form-text" name="email" /&gt; &lt;input type="submit" value="Add" class="form-submit" /&gt; &lt;/form&gt; </code></pre> <p>Here's my application.js, only change is the name of the form :</p> <pre><code>function setupAjaxForm(form_id, form_validations){ var form = '#' + form_id; var form_message = form + '-message'; var disableSubmit = function(val){ $(form + ' input[type=submit]').attr('disabled', val); }; $(form).ajaxSend(function(){ $(form_message).removeClass().addClass('loading').html('Loading...').fadeIn(); }); var options = { dataType: 'json', beforeSubmit: function(){ if(typeof form_validations == "function" &amp;&amp; !form_validations()) { return false; } disableSubmit(true); }, success: function(json){ $(form_message).hide(); $(form_message).removeClass().addClass(json.type).html(json.message).fadeIn('slow'); disableSubmit(false); if(json.type == 'success') $(form).clearForm(); } }; $(form).ajaxForm(options); } $(document).ready(function() { new setupAjaxForm('form-newsletter'); }); </code></pre> <p>URL :</p> <pre><code>urlpatterns = patterns('', url(r'^newsletter_add/$', 'views.newsletter_add', name="newsletter_add"), ) </code></pre> <p>And now the problem is with the view. Basing on the sample .php function, I've created this :</p> <pre><code>if request.method == "POST": try: e = NewsletterEmails.objects.get(email = request.POST['email']) message = _(u"Email already added.") type = "success" except NewsletterEmails.DoesNotExist: logging.debug("nie dodany") try: e = NewsletterEmails(email = request.POST['email']) except DoesNotExist: pass message = _(u"Email added successfully.") type = "success" e.save() </code></pre> <p>Result of this is when I click submit button, I get the 'Loading..." text and then nothing. Submit button is constantly disabled (until I restart browser) but my email address is added. There is no response and firebug shows "500 Internal Server Error" What am I doing wrong ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload