【问题标题】:Django request.POST.get() returns NoneDjango request.POST.get() 返回无
【发布时间】:2020-08-09 00:47:30
【问题描述】:

我遇到了一个问题,但我找不到解决方案。

我正在尝试在登录后重定向到上一页。提交后尝试 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>

Views.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。

提前致谢。

【问题讨论】:

  • ?next=something 是查询参数。要访问查询参数,您可以使用request.GET.get('next')。这应该有效。

标签: python django urlencode django-request


【解决方案1】:

点击以下按钮将发送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 模板。

您正在为 POST 请求解析 request.POST.get('next')。但是

中没有下一个
<form action="{% url 'login' %}" method="POST"> 

你需要你的form标签看起来像

<form action="{% url 'login' %}next={{ next }}" method="POST">

要解决上述问题,您需要将“next”解析为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">

【讨论】:

    【解决方案2】:

    好的,所以我没有确保将下一个值传递到登录表单中,因此解决方案是添加隐藏输入以获取请求中的下一个值:

    <input type="hidden" name="next" value="{{ request.GET.next }}" />
    

    【讨论】:

      猜你喜欢
      • 2020-02-14
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2013-09-10
      • 2014-05-14
      • 2016-03-22
      相关资源
      最近更新 更多