2017-02-12 3 views
1

views.pyDjangoは、ユーザがどのように私は次の操作を実行しないページ

class DashboardNewProfile(LoginRequiredMixin, CreateView): 
    form_class = ProfileForm 
    model = Profile 
    queryset = Profile.objects.all() 
    template_name = 'profile/profile_create.html' 

    def _usercheck(self): 
     u = self.request.user 
     qs = Profile.objects.all().filter(u) 
     if qs is None: 
      return HttpResponseRedirect('/profile/create/') 
     else: 
      return HttpResponseRedirect('/profile/view/') 

    def get_form_kwargs(self): 
     kwargs = super(DashboardNewProfile, self).get_form_kwargs() 
     kwargs.update({'user': self.request.user}) 
     kwargs.update({'slug': self.request.user.username}) 
     return kwargs 

urls.py

url(r'^profile/create/$', views.DashboardNewProfile.as_view(), name="new_profile"), 

にアクセスすることを許可しませんか? ログインしているユーザーが、すでにデータベース内のプロファイルオブジェクトに関連付けられているプロファイルを持っている場合。 ( '/ profile/view /')

答えて

0

LoginRequiredMixinが変更するためには、ユーザーが自動的に別のURL( '/ profile/view')にリダイレクトされるように、

def has_no_profile(user): 
    # Receives the request.user object from user_passes_test 
    # Note that you need to filter using model_field=value 
    # Returns true if the user has no profile, false otherwise 
    return not Profile.objects.filter(user=user).exists() 

# Method decorator attaches the user_passes_test to the class based view 
@method_decorator(user_passes_test(has_no_profile, login_url='/profile/view/'), name='dispatch') 
class DashboardNewProfile(CreateView): 
    ... 

Documentation for user_passes_test

Documentation for method_decorator

:。 user_passes_testデコレータは、ユーザーがログインしている場合、それがチェックされ dispatchメソッドをオーバーライドする必要がありますそれはあなたが本当に欲しいもののように見えていますもあることを

Documentation for filtering

UserPassesTestMixinご希望の場合、私はちょうど一般的にこれらのテストはそう、彼らは他のビューで使用できる分離し、デコレータの外観を好む保ちます。

+0

これは動作しますが、ユーザーがテストに失敗した場合、別のページにリダイレクトします。私は "login_url = '/ profile/view /'"を実行できますが、 "/ profile/view /?/ next/profile/create /"私はちょうど必要です "/ profile/view/ –

+0

to 'user_passes_test'には、 'redirect_field_name'をNoneに設定して削除することができます –

+0

あなたはこれに答えることができますhttp://stackoverflow.com/questions/42193018/redirect-after-login-django-registration-redux –

関連する問題