我已成功设置应用程序将我发送到 url 之类的
http://localhost:8000/log_in/?next=/task/1/
Run Code Online (Sandbox Code Playgroud)
我已将上下文处理器定义如下
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
Run Code Online (Sandbox Code Playgroud)
但是当我登录时,它将我重定向到主页,我request在我的登录视图中打印了它,我得到了GET一个空查询集,它是搁浅的,任何想法为什么!?
我也尝试添加
<input type="hidden" name="next" value="{{next}}" />
Run Code Online (Sandbox Code Playgroud)
到 login.html,但这将POST请求的 next 添加为空参数
编辑 1
登录视图,我尝试打印 'next' 参数并返回 null,因为它GET是一个空的查询集
def log_in(request):
"""
This method checks for the input username(email) and password.
If they are empty the user id redirected to the login page, else checks whether
the user is authenticated. If the user is not found in the database, it renders the login
page with an error_message that the email or password are invalid. Further, it checks if the user's
account is activated, it not he is redirected back to login and notified.
If the user is activated, it checks for the field remember_me, if it is not checked, then the
expiry date of the session is set to when the browser is closed.
Then the user is logged in using the login in django backends and it checks on the session
value if it is his first time or not to open the getstrated instead of the normal profile.
"""
print request.GET['next']
base_login=True
if request.POST.has_key('username') and request.POST.has_key('password'):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is None:
error_message = "The Email/Password is incorrect."
return render(request, 'userprofiles/registration.html', {'error_message': error_message})
else:
if user.is_active:
if not request.POST.get('remember_me', False):
request.session.set_expiry(0)
login(request, user)
request.session['DEPLOYED_ADDRESS'] = settings.DEPLOYED_ADDRESS
first_week = False
logged_user = UserProfile.objects.get(username=request.user.username)
if (((request.user.last_login - request.user.date_joined).days) < 8) and (logged_user.got_started == False):
first_week = True
if first_week:
return redirect('/getting_started/', RequestContext(request))
else:
return redirect('/', RequestContext(request))
else:
error_message = "This Email Hasn't Been Activated Yet. Please Check The Activation Mail Sent To You."
return render(request, 'userprofiles/registration.html', {'error_message': error_message})
else:
RegistrationForm = get_form_class(up_settings.REGISTRATION_FORM)
if request.method == 'POST':
form = RegistrationForm(data=request.POST, files=request.FILES)
if form.is_valid():
new_user = form.save()
username = form.cleaned_data['username']
password = form.cleaned_data['password']
request.session['login'] = "first"
# Automatically log this user in
if up_settings.AUTO_LOGIN:
if up_settings.EMAIL_ONLY:
username = form.cleaned_data['email']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
calculate_profile_completion(request)
return redirect(up_settings.REGISTRATION_REDIRECT)
else:
form = RegistrationForm()
return render(request, 'userprofiles/registration.html', {'form': form,'base_login': base_login})
Run Code Online (Sandbox Code Playgroud)
小智 6
当您想要呈现表单时,浏览器会向您的 Django 应用程序发出 GET 请求(这与request.GETand无关request.POST,它们仅包含分别作为 URL 参数或表单编码数据发送的数据,并且始终可用)。由于您next在 GET 请求期间使用了URL 参数,因此您将可以访问它。但是,您随后将表单页面呈现为对请求的响应,而不包含next任何地方,因此浏览器不再可以访问此值,您的应用程序也无法访问。
当浏览器继续提交表单时,它通过执行 POST 请求来完成。碰巧您使用相同的处理程序来处理此请求,但它仍然是一个单独的请求,因此,next您没有通过表单或 URL 参数传递的 .
您可以在原始 GET 请求中修复此问题(在向用户显示表单之前)。收到nextURL 参数后,您可以执行以下操作之一:
通过上下文将值传递给模板并将其放入登录表单中,并在表单中呈现隐藏字段:
<!-- inside your FORM element -->
<input type="hidden" name="next" value="{{ next }}">
Run Code Online (Sandbox Code Playgroud)
在 GET 请求期间将 next 放入会话中:
request.session['next'] = request.GET.get('next', '/')
Run Code Online (Sandbox Code Playgroud)
还有更多的方法(例如在登录表单类中添加隐藏字段),但以上两种最直接。
| 归档时间: |
|
| 查看次数: |
4166 次 |
| 最近记录: |