Django - Go to the #id tag inside the HTML.

From views.py, is there any way to make it jump to the id tag inside the HTML?

For instance,

my html: <body> <div id="lotofstuff"> content </div> <div id="pictures"> content </div> </body>

In special cases, I want the answer to go to http: //www.url.com#pictures , so it falls in the middle of the page.

Thanks.

+4
source share
2 answers

Without redirection, I would set the variable in your view and pass it to your template.

 gotodiv = False if somecase: gotodiv = 'pictures' 

Then, in your template, you have simple javascript to go into the div.

 {% if gotodiv %} <script> window.location.hash = '#{{gotodiv}}'; </script> {% endif %} 

No extra views on your web server or django app.

* Edited to change location.hash.

+6
source

I don't know if this is possible from the view (maybe yes), but the easiest way I can imagine is to use a redirect from the Template, so you might have:

 YourView: [...] if condition1: context.update('center_in_the_middle':True) else: context.update('center_in_the_middle':False) [...] 

And then in the templates: template0.html

 {% if center_in_the_middle %} [redirect to URL1] {% else %} [redirect to URL2] {% endif %} 

Hope this helps!

0
source

All Articles