python - Django request.POST.get() 返回无
问题描述
我面临一个问题,我找不到解决方案。
我正在尝试在登录后重定向到上一页。提交后尝试 request.POST.get() 时, ?next=request.path 以某种方式返回 none。
这是我的 Html 代码,它将用户引导到登录页面,将 request.path 作为“登录后的下一页”值。
{% if user.is_authenticated %}
<button class="btn" data-toggle="modal" data-target="#inquiryModal">
<a class="btn btn-primary border rounded-0"
role="button" href="#">Make an Inquiry</a>
</button>
{% else %}
<a class="btn btn-primary border rounded-0" role="button"
href="{% url 'login' %}?next={{ request.path|urlencode }}"
>Make An Inquiry</a>
{% endif %}
这是登录页面的html代码。
<div class="login-clean" style="background-color: #fff;">
<form action="{% url 'login' %}" method="POST">
{% csrf_token %}
<!--- ALERTS -->
{% include 'partials/_alerts.html' %}
<div class="form-group">
<input class="form-control" type="email" name="email" placeholder="Email"></div>
<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit">Log In</button>
</div>
</form>
</div>
视图.py 文件
def login(request):
if request.method == 'POST':
email = request.POST['email']
password = request.POST['password']
valuenext = request.POST.get('next')
print(valuenext)
user = auth.authenticate(username=email, password=password)
# if user is found and not from listing page login and redirect to dashboard
if user is not None and valuenext == "":
auth.login(request, user)
messages.success(request, 'You are now succesfully logged in')
return redirect('dash_inquiries')
# if user is found and from specific listing page login and redirect to the listing
elif user is not None and valuenext != "":
auth.login(request, user)
print("success")
messages.success(request, 'You are now logged in')
return redirect(valuenext)
else:
messages.error(request, 'Invalid credentials')
return redirect('login')
else:
return render(request, 'accounts/login.html')
我在这里做错了什么?指向登录页面时,下一个值在 url 中传递,但我似乎没有正确 get() 后端中的下一个值,因为它一直返回 None。
提前致谢。
解决方案
单击以下按钮将发送GET
请求。
<a class="btn btn-primary border rounded-0" role="button"
href="{% url 'login' %}?next={{ request.path|urlencode }}">Make An Inquiry</a>
此获取请求将呈现accounts/login.html
模板。
您只解析request.POST.get('next')
请求POST
。但是没有下一个
<form action="{% url 'login' %}" method="POST">
你需要你的form
标签看起来像
<form action="{% url 'login' %}next={{ next }}" method="POST">
要解决上述问题,您需要解析 'next' for request.GET
,并将其添加到context
响应中。
if request.method == 'POST':
# handle POST
else:
next = request.GET.get('next', '')
context = {'next': next}
return render(request, 'accounts/login.html', context=context)
然后,将其添加next
到form.action
.
<form action="{% url 'login' %}next={{ next }}" method="POST">
推荐阅读
- vmware - 在虚拟机上使用 esxcli 关闭 ESXi 主机以解决 UPS 电源故障
- android - 为什么 ActivityManager.getAppTasks 不返回所有应用任务?
- arrays - 使用 typescript 扩展通用对象输入的通用键
- php - Laravel 8 Seeders:此集合实例上不存在属性 [id]
- javascript - Firebase Firestore:在集合中创建集合
- angular - 从Angular中的数据列表中获取选定的对象
- javascript - 返回存储数据时出现 TypeError
- regex - 如何在 Notepad++ 中将连字符与正则表达式合并?
- kotlin - 迭代器的运算符符号 - Kotlin
- java - Harmony OS 中 Android OS 中 ViewParent.requestDisallowInterceptTouchEvent() 的替代方法是什么?