![](/img/trans.png)
[英]In a LoginRequiredMixin How to receive the redirect_field_name
[英]LoginRequiredMixin with redirect_field_name not redirecting
使用LoginRequiredMixin,它不會路由到我指定的“ redirect_field_name”。 即使我看到它出現在url中。
我試過將URL('user / update /')和命名URL('accounts:profile-update')都放在帶有'LoginRequiredMixin'的'redirect_field_name'中,但似乎都無法使重定向工作。
當我使用以下內容時,我可以在src / settings.py中獲得重定向以起作用LOGIN_REDIRECT_URL = '/user/'
但是我想為不同的視圖提供自定義重定向。
class ProfileUpdateView(LoginRequiredMixin, UpdateView):
# http://127.0.0.1:8000/user/update/
login_url = '/login/'
redirect_field_name = 'accounts:profile-update'
# redirect_field_name = '/user/update/'
# neither works above
model = Profile
fields = ['first_name', 'last_name', 'mobile_phone',]
template_name = 'accounts/profile_update.html'
success_url = reverse_lazy('accounts:my-profile-detail')
# PK required in UpdateView, making context['object']
def get_object(self, queryset=None):
if self.request.user.is_authenticated:
queryset = Profile.objects.get(user=self.request.user)
return queryset
# accounts/urls.py
app_name = 'accounts'
urlpatterns = [
# ... some more urls ...
path('update/', ProfileUpdateView.as_view(), name='profile-update'),
]
# src/urls.py
urlpatterns = [
path('', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
path('user/', include('accounts.urls')),
]
# src/settings.py
# LOGIN_REDIRECT_URL = '/user/'
# If I uncomment this, it works, but all my login's redirect only to this URL if I use LoginRequiredMixin :(```
您誤解了redirect_field_name
功能。
redirect_field_name
控制字段的名稱,而不是您重定向到的URL。 例如,設置redirect_field_name = 'nextpage'
意味着LoginRequiredMixin
將重定向到/login/?nextpage=/user/update/
而不是/login/?next=/user/update/
。
您通常不需要覆蓋redirect_field_name
。 堅持使用默認值next
更容易。
登錄后, LoginRequiredMixin
應該會自動重定向到上一頁。如果在刪除redirect_field_name
試之后仍未自動重定向到上一頁,則問題可能出在登錄視圖或模板中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.