[英]How can I redirect to user requested url after login - Django loginrequiredmixin
我正在尝试使用Django LoginRequiredMixin限制对视图的访问。 如果用户未登录,它将重定向到登录页面。 但是问题是登录后没有重定向到用户键入的url,而是转到默认页面?
例如,用户尝试登录site / site-create页面。 如果用户未登录,则转到登录页面。 登录后进入默认的登录页面,这里我想将用户重定向到site / site-create页面。
class CreateSite(LoginRequiredMixin, CreateView):
model = Site
fields = ['name','site_category','description',]
登录/ views.py
class LoginViewClass(FormView):
"""
Provides the ability to login as a user with a username and password
"""
form_class = AuthenticationForm
template_name = 'login/login_view_class.html'
def get_context_data(self, **kwargs):
context = super(LoginViewClass, self).get_context_data(**kwargs)
create_all_menus(ALL_MENUS)
return context
def post(self, request):
username = self.request.POST['username']
password = self.request.POST['password']
user = authenticate(username=username, password=password)
编辑->登录views.py添加了怎么办? 谢谢
LoginRequiredMixin
将重定向到login_url
并将原始URL添加为参数redirect_field_name
(链接到代码) 。
login_url
和redirect_field_name
是可以在视图中定义的两个属性,如下所示:
class MyView(LoginRequiredMixin, View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
此示例将重定向到/login/?redirect_to={current_url}
如果未指定,则这两个字段具有默认值:
在您的情况下,您的登录视图似乎没有next
URL参数。 这两个选项是:
next
URL参数。 redirect_field_name = 'your_field_name'
更改视图以发送正确的重定向字段名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.