Django中的login_required装饰器不起作用

时间:2018-03-09 12:51:44

标签: python django django-authentication django-login

我正在开发一个Django(1.10)项目,我需要为登录用户加载一个模板,该模板来自个人资料模板。我需要加载 generateCert.html 仅对于登录用户,此模板应显示与配置文件模板相同的导航栏,因为这两个模板都具有相同的标题。

这是我的generateCert.html模板:

{% load staticfiles %}
{% load static from staticfiles %}
{% load mathfilters %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NAMI Montana | User's Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content=""/>
<meta name="author" content="http://webthemez.com"/>
<!-- css -->
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet"/>
<link href="{% static 'css/fancybox/jquery.fancybox.css' %}" rel="stylesheet">
<link href="{% static 'css/flexslider.css' %}" rel="stylesheet"/>
<link href="{% static 'css/style.css' %}" rel="stylesheet"/>
<script src="https://www.paypalobjects.com/api/checkout.js"></script>

<!--Pulling Awesome Font -->
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]><![endif]-->
<style>
    input[type="image"] {
        padding-left: 30%;
        padding-top: 5%;
        font-size: 1em;
        color: #fff;
        background: transparent;
        border: 1px solid #fff;
        font-weight: bold;
        width: 70%;
}
</style>
</head>
<body>
<div class="topbar">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <p class="pull-left hidden-xs"><i class="fa fa-envelope"></i><span>matt@namimt.org </span></p>
                <p class="pull-right"><i class="fa fa-phone"></i>Tel No. (406) 443-7871</p>
            </div>
        </div>
    </div>
</div>
<!-- start header -->
<header>
    <div class="navbar navbar-default navbar-static-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="{% url 'home' %}"><img src="{% static 'images/logo.png' %}" alt="logo"
                                                                     width="190px" height="50px;"/></a>
            </div>
            <div class="navbar-collapse collapse ">
                <ul class="nav navbar-nav">
                    {% if not user.is_authenticated %}
                        <li><a href="{% url 'home' %}">Home</a></li>
                        <li><a href="{% url 'about' %}">About</a></li>
                        <li><a href="#">Blog</a></li>
                        <li><a href="{% url 'users:login' %}">Login</a></li>
                        <li><a href="{% url 'users:signup' %}">SignUp</a></li>
                        <li><a href="{% url 'contact' %}">Contact</a></li>
                    {% endif %}
                    {% if user.is_authenticated %}
                        <li><a href="{% url 'home' %}">Home</a></li>
                        <li><a href="{% url 'users:dashboard' %}">Dashboard</a></li>
                        <li class="active"><a href="{% url 'users:profile' %}">Profile</a></li>
                        <li><a href="{% url 'users:logout' %}">Logout</a></li>
                        <li><a href="{% url 'contact' %}">Contact</a></li>
                    {% endif %}
                </ul>
            </div>
        </div>
    </div>
</header>
<!-- end header -->
<div class="container">
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8" style="margin-top: 5%">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h4> You have to pay <b> $95 </b> to generate your certificate.</h4>
                </div>
                {% block content %}
                    {{ form.render }}
                {% endblock %}
            </div>
        </div>
    </div>
</div>
{% include 'footer.html' %}

以下是我的views.py:

@login_required
def payment_process(request):
if request.user.is_authenticated():
    minutes = int(request.user.tagging.count()) * 5
    testhours = minutes / 60
    hours = str(round(testhours, 3))
    # pdb.set_trace()
    # What you want the button to do.
    invoice = generate_cid()
    paypal_dict = {
        "business": settings.PAYPAL_RECEIVER_EMAIL,
        "item_name": "Certificate of Completion from Nami Montana",
        "custom": {"name": str(request.user.first_name + ' ' + request.user.last_name),
                   "hours": str(hours)},
        "invoice": str(invoice),
        "amount": "5.00",
        "notify_url": "https://8bf57d43.ngrok.io/users/paypal/",
        # "return_url": "https://b906ef46.ngrok.io/users/profile/",
        "cancel_return": "https://8bf57d43.ngrok.io/users/cancel/",
    }
    print(paypal_dict)
    # Create the instance.
    form = PayPalPaymentsForm(initial=paypal_dict)
    context = {"form": form}
    return render_to_response("users/generateCert.html", context)
else:
    return HttpResponseRedirect('/users/login')

这是我的urls.py:

   url('^process/$', views.payment_process, name='payment'),
  

generateCert.html 模板显示未登录用户菜单的匿名用户菜单,这意味着此模板由未经身份验证的用户加载。如何将此模板限制为仅从配置文件页面中登录用户。

1 个答案:

答案 0 :(得分:1)

不,这根本不是什么意思。它的意思是你没有将用户传递给模板。这是因为您正在使用render_to_response - 除了其他任何内容之外的其他内容 - 而不是运行上下文处理器的render快捷方式。

注意,您的login_required装饰器会使if request.user.is_authenticated()检查毫无意义;用户永远不会被认证。

另请注意,您确实应该使用模板继承将top nav和HTML样板文件分解为自己的模板。