Django:int() 参数必须是字符串或数字,而不是 'list'

Django:int() argument must be a string or a number, not 'list'

这可能是一个已经问过的问题错误,但在我的情况下错误是相同的,但不同的问题, 大家好,我已经编写了一个 Django 应用程序,其中我有一组模板,它们是

  1. Login.html
  2. Logout.html
  3. dashboards.html

在登录表单中验证后,用户将被转发到 dashboards.html。 我的问题是,如果用户直接尝试 dashboards.html 而没有通过 登录页面 ,那么我会收到此错误。如果他试图直接访问 dashboards.html,我想显示用户 login.html 而不是这个。提前致谢。

我的views.py长得像

from django.shortcuts import render
from django.contrib.auth.models import User, Group
from django.contrib.auth import authenticate, login, logout
from ow_dashboard.models import Dashboard
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render
from django.shortcuts import render_to_response

def custom_login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('dashboards')        
    return login(request, 'login.html', authentication_form=LoginForm)

def custom_logout(request):
    return logout(request, next_page='/')

def user(request):
    groups = request.user.groups.all()
    if request.user.is_anonymous():
        groups = []
    dashboards = Dashboard.objects.filter(owner=groups)

    context = {
        'user': request.user,
        'groups': groups,
        'dashboards': dashboards,
    }
    return render_to_response('registration/dashboards.html', context, context_instance=RequestContext(request))

# Create your views here.

和dashboards.html就像

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>*OXI-KIBANA*</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
{% block content %}
<div class="container-fluid" style="background-color: #eee; height:60px;">
  <div class="row" style="margin-top:1%">
    <div class="col-sm-3"><a href="http://www.mpowersoft.com/"><img src="{% static "bootstrap/css/logo4.png" %}" alt="MPOWER"/></a></div>
    <div class="col-sm-3"></div>
    <div class="col-sm-4" style="padding-right:4%;"><span style="margin-left:125%; ">
      {% if user.is_authenticated %}

    {{ user.first_name }}
       {{ request.user.username }}</span>
{% elseif user.is_anonymous%}
    <p>Welcome, new user. Please <a href="login.html">log in</a>.</p>
{% endif %}</div>
<div >
<span style="margin-left:9%;">
<a href="{% url 'django.contrib.auth.views.logout' %}"><button type="button" class="btn btn-xs btn-primary">sign out</button></a></span>
</div>
</div>

<div class="row" style="background-color: #eee; width:19%; height:968%; margin-top:10px;">
<div class="col-sm-2" style="background-color: #eee;">Dashboards
{% for Dashboard in dashboards %}
<strong><a href="#">{{ Dashboard.d_name }}</a><strong>
{% endfor %}
</div>  
</div>



<div class="right-contain" style="background-color: #eee; margin-top:-581px; margin-left:18%; width:83%; height:968%;">
kibana
</div>


<div class="row" style="background-color: #eee;  margin-top:3px;"><marquee>Footer</marquee>

</div>
</div>
{% endblock %}
</body>
</html>

并且urls.py是

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.auth.models import User

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/$', 'django.contrib.auth.views.login',),
    url(r'^logout/$', 'django.contrib.auth.views.logout'),
    url(r'^dashboards/$', 'ow_dashboard.views.user'),
)

您似乎没有问您的实际问题。你提到的错误是因为如果用户是匿名的,你似乎明确地将 groups 设置为一个空列表。但是您也很清楚您实际想要发生什么,即将用户发送到登录页面。那你为什么不问这个?

正如 Raja 所说, 问题的解决方案是在视图上放置一个 @login_required 装饰器。

是的,和丹尼尔说的一样。你可以 运行 下面的代码来得到结果。

@login_required(login_url='/login/')
def user(request):
    groups = request.user.groups.all()
    if request.user.is_anonymous():
        groups = []
    dashboards = Dashboard.objects.filter(owner=groups)

    context = {
        'user': request.user,
        'groups': groups,
        'dashboards': dashboards,
    }
    return render_to_response('test2.html', context, context_instance=RequestContext(request))