0

Suppose I have

def bar(request):
    template = loader.get_template('activation/bar_chart.html')
    context = RequestContext(request,{'name':'bar_chart'})
    return HttpResponse(template.render(context))

I want to send a http get request via the javascript in the template

$.get('/bar/')

But it does not render the bar_chart.html, I still stay in the current html page.

If I use the load function in the jquery

$('body').load('/bar/')

then the content of bar_chart.html will replace the body of the current html page. But I want to go to a new page (that is, the url should be /bar)

How can I do that with django and jquery?

Thank you

1

2 Answers 2

1

If you want to go to the /bar/ page you just need to change the location property. JQuery is not needed here:

location.href = "/bar/";
Sign up to request clarification or add additional context in comments.

3 Comments

That won't actually render the view via JS though...it'll just handle the redirect via JS. It sounds like the OP wants to load the /bar/ data into the current template, and then update the URL via the history api.
@rnevius but why would they want to do that?
I want to direct to the new page of the URL, not just load the page. Where can I set location.href?
0

I think i've run into this issue before as well. If I remember correctly you can do the following

return HttpResponse(template.render(context), mimetype='application/json')

2 Comments

So I can use $.get('/bar/'), then the current page will direct to the /bar/?
I'm sorry i missed that part. I think what you want is basically use pjax this library https://github.com/defunkt/jquery-pjax. . A lot better than managing html5 states.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.