首页 > 解决方案 > 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。

提前致谢。

标签: pythondjangourlencodedjango-request

解决方案


单击以下按钮将发送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)

然后,将其添加nextform.action.

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

推荐阅读