views.pyから、HTML内のidタグに移動する方法はありますか?
例えば、
私のhtml:
<body>
<div id="lotofstuff"> content </div>
<div id="pictures"> content </div>
</body>
特別な場合には、応答をhttp://www.url.com#picturesに移動させたいので、ページの中央に移動します。
ありがとうございました。
views.pyから、HTML内のidタグに移動する方法はありますか?
例えば、
私のhtml:
<body>
<div id="lotofstuff"> content </div>
<div id="pictures"> content </div>
</body>
特別な場合には、応答をhttp://www.url.com#picturesに移動させたいので、ページの中央に移動します。
ありがとうございました。
リダイレクトせずに、ビューに変数を設定してテンプレートに渡します。
gotodiv = False
if somecase:
gotodiv = 'pictures'
次に、テンプレートで、divに移動するための簡単なJavaScriptをいくつか用意します。
{% if gotodiv %}
<script> window.location.hash = '#{{gotodiv}}'; </script>
{% endif %}
Webサーバーやdjangoアプリに余分なヒットはありません。
*location.hashを動的にするために編集されました。
ビューからこれが可能かどうかはわかりませんが(おそらくそうです)、私が想像できる最も簡単な方法は、テンプレートからのリダイレクトを使用することです。
YourView:
[...]
if condition1:
context.update('center_in_the_middle':True)
else:
context.update('center_in_the_middle':False)
[...]
そして、テンプレートで:template0.html
{% if center_in_the_middle %}
[redirect to URL1]
{% else %}
[redirect to URL2]
{% endif %}
それが役に立てば幸い!