Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在我的Django应用程序中。尽管传递了@login\u required,但某些页面上不存在用户对象_Python_Django_Authorization - Fatal编程技术网

Python 在我的Django应用程序中。尽管传递了@login\u required,但某些页面上不存在用户对象

Python 在我的Django应用程序中。尽管传递了@login\u required,但某些页面上不存在用户对象,python,django,authorization,Python,Django,Authorization,所以问题随之而来 我有几个页面的应用程序。在顶部,我有一个带有标准登录/注销和登录表单的菜单。此菜单位于my base.html中。如果您已经登录或尚未登录,它将显示正确的选项。 应用程序中的所有页面均受@login\u required保护 base.html <ul class ="nav navbar-nav navbar-right"> {% if request.user.is_authenticated %} <li ><a href="

所以问题随之而来

我有几个页面的应用程序。在顶部,我有一个带有标准登录/注销和登录表单的菜单。此菜单位于my base.html中。如果您已经登录或尚未登录,它将显示正确的选项。 应用程序中的所有页面均受@login\u required保护

base.html

<ul class ="nav navbar-nav navbar-right">
  {% if request.user.is_authenticated %}
      <li ><a href="{% url 'auth_logout' %}" %>  {{user}} Logout  </a></li>
  {% else %}
  <li ><a href="{% url 'registration_register' %}" %> Register </a></li>
       {% endif %}
  <li><div class="form">
        {% if not request.user.is_authenticated and not "/accounts/login" in request.get_full_path %}

          <form class='navbar-form navbar-right' method='POST' action='{% url "auth_login" %}'>{% csrf_token %}
              <div class='form-group'>
                  <input type='text' class='form-control' name='username' placeholder='Username' /> 
              </div>
              <div class='form-group'>
                  <input type='password' class='form-control' name='password' placeholder='Password' />
              </div>
              <button type='submit' class='btn btn-default'>Login</button>
          </form>
        {% endif %}
  </li>
</ul>
我认为联系人列表模板与此无关,因此我不会发布模板代码

你知道原因是什么吗


谢谢

请使用“渲染”而不是“渲染到”响应。您没有向模板发送请求对象,因此“request.user.is\u authenticated”将不起作用。

请使用render而不是render\u to\u响应。您没有向模板发送请求对象,因此“request.user.is\u authenticated”将不起作用

    @login_required
    def contact_list(request):
        contact_list = ContactFilter(request.GET, queryset=Contacts.objects.all())
        #return render_to_response('my_app/template.html', {'filter': filter})
        #company_list = Company.objects.all()
        paginator = Paginator(contact_list, 25) # Show 25 contacts per page

        page = request.GET.get('page')
        try:
            contact = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            contact = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            contact = paginator.page(paginator.num_pages)

        #return render(request, 'customer/company_list.html', {'company': company})
        return render_to_response('customer/contact_list.html', {'contact': contact_list,'page_contact':contact })