javascriptjqueryajaxdjangorender-to-response

How can i handle render_to_response with ajax, so that the page could be redirected with the content?


views.py

def usersignup(request):
        content = {}
        userinfo = user_signup.objects.all()
        if request.method == "POST":
            fullname = request.POST.get('fullname')
            email = request.POST.get('email')
            password = request.POST.get('password')
            for i in userinfo:
                if i.user_email == email:
                    # content['err_msg'] = 'Email Id already exists'
                    return HttpResponse('Already Exist')
                    # return render_to_response("index.html",content,context_instance=RequestContext(request))
            userobj = user_signup.objects.create(full_name=fullname, user_email=email, user_password=password)
            content['fullname'] = fullname
            content['useremail'] = email
            request.session['user'] = userobj.user_email
        return render_to_response("users/user_signup.html",content,context_instance=RequestContext(request))

index.js

function usersignup()
{
    var csrftoken = getCookie('csrftoken');
    emailid = document.getElementById('nuemail').value
    password = document.getElementById('nupassword').value
    fullname = document.getElementById('nufullname').value

    alert(emailid);
    alert(password);

     $.ajax({
        url: '/usersignup/',
        type: 'POST',
        data : {
        // CSRF : csrftoken,
        email : emailid,
        password : password,
        fullname : fullname
    },
    }).done(function(response){
        alert(response);
        if (response == "Already Exist")
        {
            $('#p2').empty();  
          alert(response);  
          $('#p2').append('EmailId already exists');
        }
  });
}

index.html

<div class="md-modal " id="modal-19"> <!--  add flip efffect -->
        <div  class="md-content modal-form"  id="usersignup">
            <h3>Sign Up <img alt="" class="md-close right" onclick="closeAll()" src="/static/img/close_pink.png" ></h3>
            <div>
                <section class="modal-inside">
                <div class="row">
                    <div class="columns large-12">
                        <div class="group">
                            <input type="text" name="fullname"  id= "nufullname" required> <span class="highlight"></span>
                            <span class="bar"></span> <label>Full name</label>
                        </div>

                    </div>

                </div>
                <div class="row">
                    <div class="columns large-12">
                        <div class="group">
                            <input type="email" name="email" title="In this format 'aaaaaa@bbbb.ccc'"
                                id = "nuemail" required> <span
                                class="highlight"></span> <span class="bar"></span> <label>Email
                                address</label>
                        </div>

                    </div>
                </div>
                <div class="row">
                    <div class="columns large-12">
                        <div class="group">
                            <input type="password" name='password' id = "nupassword" required> <span class="highlight"></span>
                            <span class="bar"></span> <label>Password</label>
                        </div>
                    </div>
                </div>
                <div class="row">
                        <div class="columns large-8">
                            <h6>
                                Have an account? <a class=" md-close md-trigger"
                                    onclick="userLogIn()" data-modal="user-sign-in"> Log in</a>
                            </h6>
                        </div>
                        <div class="columns large-4">
                            <button class="tiny round right" onclick="usersignup();">SIGN UP</button>
                        </div>
                        <p id="p2"></p>
                    </div>
            </section>


            </div>
            </div>

I have added three files, so my problem is that whenever i am creating a new user i want the page should be redirected to ("users/user_signup.html") for which i used render_to_response.

I have used the ajax function also, in index.js where in i just wanted to check the response that if email-id already exist it should return the Httpresponse as ("Already Exist") it is working fine.

The problem with the code is that , in case of render_to_response the page is not redirecting to the other page , instead returning the response to ajax function. Is there any solution to it that how can i simply redirect the page. Please help me TIA.


Solution

  • I was calling render_to_response using post method, on which i was getting back the response as the whole HTML page, so the solution to which i worked on was i simply called render_to_response using get method on which the page got loaded.