Django 成功 URL 附加到中间件重定向 URL 而不是重定向到成功 URL
Django Success URL Gets Appended To Middleware Redirect URL Instead of Redirecting To Success URL
我有一个带有用户名字段的表单位于 /accounts/choose-username/
。
views.py
from django.views.generic import FormView
from .forms import ChooseUsernameForm
from django.contrib.auth import get_user_model
User = get_user_model()
class ChooseUsernameView(FormView):
template_name = 'registration/choose-username.html'
form_class = ChooseUsernameForm
success_url = 'accounts/profile/' # <-------------
def form_valid(self, form):
"""If the form is valid, redirect to the supplied URL."""
print(form.cleaned_data['username'])
print(self.request.user)
user = User.objects.get(email=self.request.user)
user.username = form.cleaned_data['username']
user.save()
return super().form_valid(form)
middleware.py
from django.shortcuts import redirect, reverse
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
if request.user.is_authenticated:
if request.user.username is None:
print(request.path)
if not request.path == reverse('choose_username'):
return redirect(reverse('choose_username'))
return response
当我提交表单时,我的 url 是 accounts/choose-username/accounts/profile/
我希望它是 accounts/profile/
MIDDLEWARE
settings.py 看起来像这样:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'accounts.middleware.SimpleMiddleware', # <---
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
我的问题
为什么我的成功 URL 会附加到我的 accounts/choose-username/
URL?我该如何解决?
因为这个——accounts/profile/
——不是以斜杠开头的(/
),所以它是一个相对路径。浏览器自动将相对路径附加到当前页面的 url.
您需要使用绝对路径(开头加斜线)
success_url = '/accounts/profile/'
我有一个带有用户名字段的表单位于 /accounts/choose-username/
。
views.py
from django.views.generic import FormView
from .forms import ChooseUsernameForm
from django.contrib.auth import get_user_model
User = get_user_model()
class ChooseUsernameView(FormView):
template_name = 'registration/choose-username.html'
form_class = ChooseUsernameForm
success_url = 'accounts/profile/' # <-------------
def form_valid(self, form):
"""If the form is valid, redirect to the supplied URL."""
print(form.cleaned_data['username'])
print(self.request.user)
user = User.objects.get(email=self.request.user)
user.username = form.cleaned_data['username']
user.save()
return super().form_valid(form)
middleware.py
from django.shortcuts import redirect, reverse
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
if request.user.is_authenticated:
if request.user.username is None:
print(request.path)
if not request.path == reverse('choose_username'):
return redirect(reverse('choose_username'))
return response
当我提交表单时,我的 url 是 accounts/choose-username/accounts/profile/
我希望它是 accounts/profile/
MIDDLEWARE
settings.py 看起来像这样:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'accounts.middleware.SimpleMiddleware', # <---
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
我的问题
为什么我的成功 URL 会附加到我的 accounts/choose-username/
URL?我该如何解决?
因为这个——accounts/profile/
——不是以斜杠开头的(/
),所以它是一个相对路径。浏览器自动将相对路径附加到当前页面的 url.
您需要使用绝对路径(开头加斜线)
success_url = '/accounts/profile/'