0

我为博客文章创建了@login_required,因此用户必须登录才能看到它,但它似乎登录但出现的页面是空白的。但是,当我查看 url 时,它说我在页面上。例如“ http://127.0.0.1:8000/accounts/login/?next=/payment/ ” 当我退出该页面时,我也登录了

如果 user: login(request, user) return HttpResponseRedirect(request.POST.get('next')) 我已经在视图中尝试过

并添加

到 login.html

登录.html

{% block content %}
<div style="max-width: 300px; margin: auto;">
<h2 style="text-align: center; font-size: 45px;">Login</h2>
<form class="site-form"  method="post">
    {% csrf_token %}
    {{ form.as_p}}
    <input type="hidden" name="next" value="{{ next }}">
    <button type="submit" style="background: #FC5C00; border: 0; color: #fff; font-size: 1.3em; display: block;margin: 0 auto;">Login</button>
</form>
</div>
{% endblock content %}

视图.py

def login_view(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            #login user
            user = form.get_user()
            login(request,user)
            if user:
                login(request, user)
                return HttpResponseRedirect(request.POST.get('next'))
            return redirect('/')
    else:
        form = AuthenticationForm()
    return render(request,'accounts/login.html',{'form':form})

博客视图

@login_required(login_url='/accounts/login/')
def blog_detail(request, slug):
    #return HttpResponse(slug)#Return the slug urls for the detail pages
    blog = Blog.objects.get(slug=slug)
    return render(request, 'blog/blog_detail.html', {'blog':blog})

我需要做的就是登录到具有@login_required 标记的页面,并在登录后显示该页面

谢谢

4

0 回答 0