views.py
class CreateProfile(LoginRequiredMixin, CreateView):
login_url = '/accounts/login/'
template_name = 'profile/new_profile.html'
model = Profile
fields = ['full_name','profile_pic','title','summary']
def user_redirect(request):
if request.user.is_authenticated():
return render(request, '#redirect to some page')
如果用户已经登录,则他应该无法访问'个人资料/创建'网址。
答案 0 :(得分:2)
您可以使用以下装饰器装饰您的注册或登录视图......
from django.http import HttpResponseRedirect
def anonymous_required( view_function, redirect_to = None ):
return AnonymousRequired( view_function, redirect_to )
class AnonymousRequired( object ):
def __init__( self, view_function, redirect_to ):
if redirect_to is None:
from django.conf import settings
redirect_to = settings.LOGIN_REDIRECT_URL
self.view_function = view_function
self.redirect_to = redirect_to
def __call__( self, request, *args, **kwargs ):
if request.user is not None and request.user.is_authenticated():
return HttpResponseRedirect( self.redirect_to )
return self.view_function( request, *args, **kwargs )