Django request.POST.get()返回None

2024-09-29 19:19:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我面临一个问题,却找不到解决办法

我试图在登录后重定向到上一页。在提交后尝试请求.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中传递的,但我似乎没有正确地获取()后端中的下一个值,因为它一直不返回任何值

提前谢谢


Tags: anddivformifisemailrequestlogin
2条回答

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

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

单击以下按钮将发送GET请求

<a class="btn btn-primary border rounded-0" role="button" 
    href="{% url 'login' %}?next={{ request.path|urlencode }}">Make An Inquiry</a>

此get请求将呈现accounts/login.html模板

您正在为POST请求分析request.POST.get('next')。但没有下一个目标

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

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

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

要解决上述问题,您需要为request.GET解析“next”,并将其添加到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">

相关问题 更多 >

    热门问题