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

rnevius
  • 26,578
  • 10
  • 58
  • 86
alec.tu
  • 1,647
  • 2
  • 20
  • 41
  • possible duplicate of [Modify the URL without reloading the page](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) – rnevius Mar 25 '15 at 13:51

2 Answers2

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/";
catavaran
  • 44,703
  • 8
  • 98
  • 85
  • 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 Mar 25 '15 at 13:38
  • @rnevius but why would they want to do that? – Daniel Roseman Mar 25 '15 at 14:06
  • I want to direct to the new page of the URL, not just load the page. Where can I set location.href? – alec.tu Mar 26 '15 at 04:09
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')

harshil
  • 1,655
  • 1
  • 11
  • 7
  • So I can use $.get('/bar/'), then the current page will direct to the /bar/? – alec.tu Mar 26 '15 at 04:10
  • I'm sorry i missed that part. I think what you want is basically use pjax this library [https://github.com/defunkt/jquery-pjax](https://github.com/defunkt/jquery-pjax). . A lot better than managing html5 states. – harshil Mar 26 '15 at 13:22