Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. <form action="{% url 'some_url' %}" class="some-class" method="post"">
  2. ...
  3.     <button class="button_class"  type="submit"">Submit</button>
  4. </form>
  5.  
  6.  
  7. $('.button_class').on('click', function(e) {
  8.             e.preventDefault();
  9.             $.ajax({
  10.                 type: 'POST',
  11.                 url: '{% url 'some_url' %}',
  12.                 cache: false,
  13.                 data: {
  14.                     form: $('form[class=some-class]').serialize()
  15.                 },
  16.                 success: function(data){
  17.                     console.log('TRTRTRTRTTRTRTR')
  18.                 }
  19.             });
  20.             return false;
  21.     });
  22.  
  23. #views.py
  24.  
  25. def post(self, request, *args, **kwargs):
  26.     ...
  27.     if form.is_valid():
  28.         return self.form_valid(form)
  29.     else:
  30.         data = {
  31.                 'result': 'error',
  32.                 'message': 'Form invalid',
  33.                 'errors': form.errors.as_json()
  34.             }
  35.         return JsonResponse(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement