4

views.pyから、HTML内のidタグに移動する方法はありますか?

例えば、

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

特別な場合には、応答をhttp://www.url.com#picturesに移動させたいので、ページの中央に移動します。

ありがとうございました。

4

2 に答える 2

7

リダイレクトせずに、ビューに変数を設定してテンプレートに渡します。

gotodiv = False
if somecase:
    gotodiv = 'pictures'

次に、テンプレートで、divに移動するための簡単なJavaScriptをいくつか用意します。

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

Webサーバーやdjangoアプリに余分なヒットはありません。

*location.hashを動的にするために編集されました。

于 2012-07-17T22:48:29.867 に答える
0

ビューからこれが可能かどうかはわかりませんが(おそらくそうです)、私が想像できる最も簡単な方法は、テンプレートからのリダイレクトを使用することです。

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 %}

それが役に立てば幸い!

于 2012-07-17T22:39:56.653 に答える