title: javascript-POST request using ajax in Django issue
body: <p>I am a beginner and self learned programmer and have written a code on my own. I am attempting to use Javascript post request using ajax and facing some continuous problem. There are lot of mistakes in code and want you guys to correct it with an explanation. Here is my base.js file:</p>
  
  <pre><code>var user = {
  signup : function() {
      var firstname = document.signupform.first_name.value;
      var lastname = document.signupform.last_name.value; 
      var username = document.signupform.username.value;
      var email = document.signupform.email.value;
      var password = document.signupform.password.value;
      var confirmpassword = document.signupform.confirmpassword.value;
      if (firstname == "")
      {
          alert("Please provide your first name!")
          document.signupform.first_name.focus();
      }
      else if (username == "")
      {
          alert("Please provide the username!")
          document.signupform.username.focus();
      }
      else if (email == "")
      {
          alert("Please provide your Email!")
          document.signupform.email.focus() ;
      }
  
      else if (password == "")
      {
          alert("Please enter a valid password")
          document.signupform.password.focus() ;
      }
      else if (password != confirmpassword) 
      {
          alert("Passwords do not match.");
          document.signupform.confirmpassword.focus() ;
      }
      else
      {
          data = {"firstname":firstname, "lastname":lastname, "username":username, "email":email,"password":password};
          this.postRequest(data);
      }
      return false
  },
  
  postRequest:function(data,url,form) 
  {
      $.ajax({
                type: "POST",
                url: '',
                data: data,
                success: function(data){
                  if(data["success"])
                      {
  
                      window.location.href = '{% url home %}'
  
                      }
                  else
                      {
                          alert(data["error"])
                      }
                  }
              });
  },
  </code></pre>
  
  <p>views.py:</p>
  
  <pre><code>class UserFormView (TemplateView):
  template_name = "signup.html"
  #display signup blank form
  def get(self, request,*args,**kwargs):
      context = super(UserFormView,self).get_context_data(**kwargs)
      return self.render_to_response(context)
  
  
  #process form data
  def post(self, request,*args,**kwargs):
      context = {}
      data = request.POST
      user = get_user_model().objects.create(username=data["username"],first_name=data["firstname"],email=data["email"],last_name=data["lastname"])
      user.set_password(data["password"])
      user.save()
      user.backend = "django.contrib.auth.backends.ModelBackend"
      if user is not None:
          login(request, user)
          context["success"] = True
          # return redirect("home")
      else:
          context["success"] = False
          context["error"] = "The username is already taken!"  
  
          return self.render_to_response(context)
  </code></pre>
  
  <p>I am putting 'onclick' function in my signup.html:</p>
  
  <pre><code>&lt;a class="btn btn-success" href="javascript:void(0)" onclick="user.signup()"&gt;Sign Up&lt;/a&gt;
  </code></pre>
  
  <p>I am getting an error of csrf as well.</p>