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
  • 25,680
  • 10
  • 53
  • 79
alec.tu
  • 1,467
  • 2
  • 15
  • 37
  • 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
  • 42,728
  • 8
  • 88
  • 82
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,555
  • 1
  • 10
  • 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