2012-07-17 35 views
4

從views.py,有沒有什麼辦法讓它進入HTML標籤內的標籤?Django - 去HTML中的#id標籤

例如,

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

在特殊情況下,我想響應去http://www.url.com#pictures,所以它進入頁面的中間。

謝謝。

回答

6

如果沒有重定向,我會在您的視圖中設置一個變量並將其傳遞給您的模板。

gotodiv = False 
if somecase: 
    gotodiv = 'pictures' 

然後,在你的模板中,有一些簡單的javascript去div。

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

在您的網絡服務器或django應用程序沒有額外的點擊。

*編輯使location.hash動態。

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

希望它有幫助!