javascript - How to integrate Django form with Ajax? -


i've gone through many tutorials in how integrate ajax django form complicated. have signup model signup form , views following. models.py

from django.db import models  class signup(models.model):     name = models.charfield(max_length=120, blank=true, null=true)     email = models.emailfield()      def __unicode__(self):         return self.email 

and forms.py

from django import forms .models import signup  class signupform(forms.modelform):     class meta:         model = signup         fields = ['name', 'email']      def clean_name(self):         name = self.cleaned_data.get('name')         return name       def clean_email(self):         email = self.cleaned_data.get('email')          try:             match = signup.objects.get(email=email)         except signup.doesnotexist:             return email          raise forms.validationerror('this email address subscribed.') 

views.py

from django.shortcuts import render django.core.mail import send_mail django.conf import settings   .forms import signupform  def index(request):     form = signupform(request.post or none)      if form.is_valid():         name = form.clean_name()         email = form.clean_email()         instance = form.save()          subject = 'bruke church news'         from_email = settings.email_host_user         to_email = [email]         contact_message = "%s:thank signing our newsletter via %s. we'll in touch" %(                                          name,                                          email)          send_mail (subject,                              contact_message,                              from_email,                              to_email,                             fail_silently=false)     context = {         "form": form     }     return render(request, "index.html",context) 

and html form looks

<form action="" method="post">                   {% csrf_token %}                   {{form|crispy}}                   <input type="submit" value="sign up" class="btn btn-primary">                 </form> 

this code runs loads full page , want load form. after googling, found concept of ajax having problem in doing so. please me thank you

example of ajax post on button click submit

this method needs run

put in html

function adddata(){     var name = $("#name").val();     var email = $("#email").val();      // should extract each , every id form fields       var signupdata = { name:name, csrfmiddlewaretoken: '{{ csrf_token }}',email:email};     $.ajax({                 type: "post",                 url: "../../index/",                 data: signupdata,                 success: function(data) {                                         alert("you have sucessfully signed ");                                                                      },                                         statuscode: {                                                       500: function() {                                                                          alert("you have signed ");                                                                        }                                                       },                                     })     } 

in views.py

def index(request):      if request.method == 'post': # frontend getting data in post method , checking if front end giving post method or not          get_email = request.post.get('email') # taking data front end in form of post django  user email address         get_name = request.post.get('name')# taking data front end in form of post django name         queryset_list = signup.objects.all().values_list("email",flat=true)# performing django query , getting signup email address         if get_email in queryset_list:            return httpresponse(status=500)         else:            signup.objects.create(name=get_name,email=get_email)     return httpresponse('') 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -