0

I am following this answer to make smooth page switches: Rendering JSON objects using a Django template after an Ajax call

How do I make Django understand, that I'm on the other page already?

I want the list element to obtain 'active' class and to have /leaders/ in browser url path when I click on the link and load leaderboard template. Say i have:

leaderboard.html:

<li{% if path == leaders %} class="active"{% endif %}>  
   <a href="/leaders/">Leaderboard</a>
</li>

urls.py:

url(r'^leaders/$', 'index.views.leaderboard', name='leaders'),

view:

def leaderboard(request):
  ...
  if request.is_ajax():
    return_str = render_block_to_string('leaderboard.html', 'block_name', context)
    return HttpResponse(return_str)
  else:
    response = render_to_response('leaderboard.html', context)
    return response    

Edit: I've tried to use window.history.pushState, it changed url in browser, but did not tell anything to django.

4
  • See broweserstate/history.js on GitHub. Commented Jun 27, 2015 at 18:04
  • 1
    You don't have to tell Django. You have to tell your html/css, which Django has absolutely nothing to do with after it has initially rendered the template. Commented Jun 27, 2015 at 19:24
  • stackoverflow.com/a/1468/3033586 Commented Jun 28, 2015 at 8:07
  • @SeledkaLebedka When you change the URL, you'll need to issue an AJAX request to tell Django in order to get new data. Commented Jun 28, 2015 at 9:39

0

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.