def index(request):
the_user = request.user
Djangoでは、それが実際のユーザーであるかどうかをどのように知ることができますか?私は試した:
if the_user: ただし、「AnonymousUser」は誰もログインしていなくても存在します。したがって、常にtrueが返され、これは機能しません。
回答:
がrequest.user.is_anonymous返されるかどうかを確認できますTrue。
is_authenticated()見えます:docs.djangoproject.com/en/1.9/topics/auth/default/
is_authenticated()です。thegarywilson.com/blog/2006/is_authenticated-vs-is_anonymous
request.user.is_authenticated、Django1.10以降の属性であるを使用するis_anonymousことです。これは現在と同じです。docs.djangoproject.com / en / dev / ref / contrib / auth /…を参照してください。参照-あなたはまた、Djangoのガーディアンを使用しているならば、これらの属性は、あなたは彼らがなると思う何をしないことに注意してくださいdjango-guardian.readthedocs.io/en/stable/configuration.htmlは
の代替
if user.is_anonymous():
# user is anon user
ユーザーオブジェクトのIDが何であるかをテストすることによってです:
if user.id == None:
# user is anon user
else:
# user is a real user
https://docs.djangoproject.com/en/dev/ref/contrib/auth/#anonymous-usersを参照してください
views.pyためrequest.user.is_anonymous()、使用する必要があることに注意してください。テンプレートでは、使用する必要があります{{user.is_anonymous}}