1

私はdjangoとCBVに比較的慣れていないので、ユーザーがクラスのpostメソッド内で認証されていない場合にユーザーをリダイレクトする方法を理解しようとしていますListView。したがって、コードは次のようになります。

#views.py

from django.shortcuts import render

class MyListView(ListView):

    def post( self, request, *args, **kwargs ):
        if not request.user.is_authenticated():
             print "user NOT authenticated"
             return render(request, '/site_templates/home.html')
                         # does not redirect #
        else:
             print "user IS authenticated"
                         # do some stuff: this part works just fine #

これを試してみて、ユーザーが認証されていない場合、コードが必要なテンプレートにリダイレクトされないようです。何が欠けているのかわからない。

ここでユーザーをTemplateViewにリダイレクトすることは可能でしょうか?どんな助けでも素晴らしいでしょう。

4

1 に答える 1

0

おそらく試してみてください:

from django.http import HttpResponseRedirect

if not request.user.is_authenticated():
  return HttpResponseRedirect('desired URL')  

HttpResponseRedirectオブジェクトの前に戻ることを強調することが重要です。

于 2017-10-21T21:57:45.583 に答える