2012-09-12 36 views
-1

我需要根據用戶的登錄在我的模板,以顯示不同的按鈕的場景是我的模板顯示如下按鈕:如何根據用戶顯示模板不同的按鈕登錄在Django

Check whether the user logged in or not- 
    if user logged in- 
    check which channel he follwed: 
    if he followed any channel 
     beside that channel name show "followed" button 
    else 
     show "follow" button with the path follow_save 
    elif user not logged in: 
    show follow button with the path follow_save 

我堅持如何做到這一點?這是視圖還是模板的任務?怎麼做?從你的專家的任何幫助將節省我..我也想從會議採取user_id。 這裏是我的views.py

e= EventArchiveGallery.objects.all() 
user = request.session['user_id'] 
if user: 
    j = EventArchiveGallery() 
    joining_list = j.joiningList(user)   

return render_to_response('gallery/allevents.html', 
{ 
    'joining_list':joining_list, 
}, 
context_instance=RequestContext(request)       
) 

這是我的模板:

{% for event in live %} 
    <p>Event Title:{{event.event_title}}<p/> 
    <p>Channel Name:{{event.channel_id.channel_title}}</p> 
    <p>Event Description{{event.event_description}}</p> 
    {% if event in joining_list %} 
     <p>followed</p> 
      {%else%} 
        <p>follow</p> #I have wanted to show follow_save function call from this button,when user clicked 
      {endif%} 
    {% endfor %} 
+0

你在堅持什麼?顯示一些代碼,你有什麼嘗試? –

+0

我在我的問題中添加了一些代碼..但我認爲我的views.py是不正確的。因爲它顯示了關鍵錯誤user_id .. –

+0

@HananFaisal你有'auth'模塊在'INSTALLED_APPS'設置中。 py'? –

回答

0

你問兩個問題更好,如果你能在2個問題,反正拆...

  • 根據用戶的登錄信息在我的模板中顯示不同的按鈕
    • 更好的方法是確定在視圖中顯示的內容nd將上下文參數傳遞給模板。根據這個變量模板將呈現不同的HTML。

示例模板:假設視圖傳遞followed_channel參數。

{% if user.is_authenticated %} 
    {%if followed_channel %} 
     {# render html you want #} 
    {%else%} 
     {# render another way #} 
    {%endif%} 
{%endif%} 
  • 我也一直想在您從會議到會議,從

採取USER_ID,你必須保存它。所以你可以更新你的觀點爲

e= EventArchiveGallery.objects.all() 
user = request.session.get('user_id') 
if not user: 
    request.session['user_id'] = request.user.id 
if user: 
    j = EventArchiveGallery() 
    joining_list = j.joiningList(user)   

return render_to_response('gallery/allevents.html', 
{ 
    'joining_list':joining_list, 
}, 
context_instance=RequestContext(request)       
) 
相關問題