2016-12-13 2 views
0

ログインしたユーザーとして表示できるmy user profileビューがあります。私は2番目のビューを追加したいユーザーでログインので、他にもプロフィールページを訪問し、私は、私はdjangoの別のユーザーとしてプロフィールページを表示

urls.py

url(r'^accounts/profile/', main_views.uprofile, name='uprofile'), #the page you see as my profile 
    url(r'^profile/(?P<pk>\d+)/$', main_views.oprofile, name='oprofile'), # the page i use so other users can view the profile page 
    url(r'^accounts/update/(?P<pk>\d+)/', User_Profile_views.edit_user, name='edit_user'), #Custom update profile page 

main_viewsそれを正しい方法でやって本当にわからないことができます。ビューの製品の観点からPY

@login_required(login_url='/accounts/login/') 
def uprofile (request): 

    context = locals() 
    template = 'profile.html' 
    return render (request, template, context) 

def oprofile (request, pk): 
    user = User.objects.get(pk=pk) 

    context = locals() 
    template = 'profile.html' 
    return render (request, template, context) 
+0

これは非常に広い聞こえるが、私はあなただけのいずれかを非表示にするには、テンプレートにいくつかのブール値を含める必要が想像します編集機能 – Sayse

+0

ログインしたIDが多くのユーザーIDを必要とするため、誰も編集できません...もっと効率的な方法を探しています。 – LeLouch

+0

私はあなたが単一のブール値よりも効率的になるとは思わない。これまでに何を試しましたか? – Sayse

答えて

1

、あなたはuprofileoprofileの両方に同じURLを維持したいでしょう。 1つの簡単な理由は、私が自分のプロフィールにアクセスしたときに、他の人と共有したい場合は、URLをコピーして貼り付けてしまうだけです。

どうすればよいですか?

ビューでは、テンプレートが正しい要素をレンダリングするのに役立つフラグを渡します。たとえば、ユーザーが訪問しているプロファイルと同じ場合は、フラグeditableを渡し、編集ボタンを表示するために使用します。そして、2つのビューの代わりに、単一のビューを持つことができます。

また、idの代わりに、ユーザーはユーザー名/ハンドルを覚えている傾向があります。だから、ユーザー名を持つ方が良いです。ただし、すべてのユーザーに一意のユーザー名が設定されていることを確認してください。

urls.py

url(r'^profile/(?P<username>[\w\-]+)/$', main_views.profile, name='profile'), 

views.py

def profile (request, username): 
    # If no such user exists raise 404 
    try: 
     user = User.objects.get(username=username) 
    except: 
     raise Http404 

    # Flag that determines if we should show editable elements in template 
    editable = False 
    # Handling non authenticated user for obvious reasons 
    if request.user.is_authenticated() and request.user == user: 
     editable = True 

    context = locals() 
    template = 'profile.html' 
    return render (request, template, context) 
関連する問題