vinoth kumar
vinoth kumar

Reputation: 216

Django render to new template is not working

I am trying to render new template 'quesHome.html' via jquery ajax call. But page is not redirected to quesHome page. Also i am not getting any errors. I am a beginner to Django please correct me if i am wrong.

Below is my view:

def getSearchQuesions(req):
    print("getSearchQuesions")
    topic = Topic.objects.all()
    form = CreateQuestion()
    ques_id = req.GET['ques_id']
    question = Question.objects.filter(pk=ques_id)
    return render(req,'quesHome.html',{'topic':topic ,'question':question,'form':form})

Below is my js code:

function getFilteredQuestions(ques_id) {
    $.ajax({
        type: 'GET',
        url: '/getSearchQuesions/',
        data: { "ques_id": ques_id },
        success: function () {
            console.log("success");
        }
    });
}

Below is my urls.py:

 url(r'^getSearchQuesions/$', for_views.getSearchQuesions, name='getSearchQuesions'),

Below is my quesHome.html:

{%extends 'master_page.html'%}
{% block quesHome %}
    <div class="container" id="cont">
        <div class="row topicDiv">
            <input type="hidden" value="{%csrf_token%}" id="hid_csrf" />
            {% for t in topic %}
            <h4 id="{{t.pk}}" class="topCls"><span class="badge badge-Info">{{t.name}}</span></h4>
            &nbsp;
            {% endfor %}
        </div>
        <br />
        <div class="" id="">
            {% for q in question %}
            <div id="feed">
                <div id="">
                    <h5><span class="">{{q.name}}</span></h5>
                </div>
                <div class="">
                    <h6><span class="">{{q.answer}}</span></h6>
                </div>
            </div>
            <br />
            {% endfor %}
        </div>
    </div>
{% endblock %}

Upvotes: 0

Views: 775

Answers (2)

Rajesh Yogeshwar
Rajesh Yogeshwar

Reputation: 2179

What you are doing is making a request via ajax and your view is returning a template. You are expecting a redirection to the page with the question id. You can simply not make this ajax call and if there is a link to which you intended to bind this ajax, let it behave normally if it is an anchor tag. Because then you will be redirected without having the need for ajax call.

Upvotes: 1

HenryM
HenryM

Reputation: 5793

You're sending a return to the ajax query which is then going to try and do something with it.

Instead of using the ajax call, try something like:

window.location = '/getSearchQuesions/'+ques_id

Upvotes: 4

Related Questions