2015-10-13 83 views
0

假設我有Django的父子關係模板

  • mainprofile.html這是我從views.profile渲染和
  • followers.html這是我從views.followers渲染

當我點擊追隨者按鈕時,它會調用views.followers 並呈現追隨者並將其正確插入主配置文件。

我想要做什麼:

當我這樣做,我失去了父頁面上的所有配置文件信息(主檔次),如userinfo.username 我如何克服這個問題?

mainprofile.html:

{% extends 'layout/master.html' %} 
    {% load sharedtags %} 

    {% block content %} 

     {{ userinfo.username }} 

     ...some profile information... 

     followers_button 

     {% block right_side_main_layout %} 

      ... some info ... 

     {% endblock right_side_main%} 

    {% endblock content%} 

followers.html:

{% extends 'mainprofile.html' %} 
{% block right_side_main_layout %} 

    ... list of followers ... 

{% endblock right_side_main_layout %} 

views.py:

def profile(request,username): 

    userdetail = AuthUser.objects.filter(username=username) 

    template='profil/mainprofile.html' 
    context = RequestContext(request, {'userinfo': userdetail }) 
    return render_to_response(template, context) 

def followers(request, username): 

    followers = Followers.objects.filter(username=username) 

    template = 'profil/em-profile-follower.html' 
    context = RequestContext(request,{'followers': followers}) 
    return direct_to_template(template,context) 

感謝。

回答

1

您必須將AuthUser實例也添加到followers視圖。

def followers(request, username): 

    followers = Followers.objects.filter(username=username) 
    userdetail = AuthUser.objects.filter(username=username) 

    template = 'profil/em-profile-follower.html' 
    context = RequestContext(request,{'followers': followers, 'userinfo': userdetail}) 
    return direct_to_template(template,context) 

現在,你還必須在這個模板(和視圖)

的用戶信息,我不知道你的模型的外觀。但我認爲AuthUser有一個ForeignKeyField(Follower)。 如果你把這個領域的related_name(如「追隨者」),你可以這樣做: userdetail.followers

userdetail = AuthUser.objects.filter(username=username)

,並在您的模板然後你可以通過做訪問你的追隨者