Django - tables2 STATIC URL不起作用

时间:2015-09-22 21:24:33

标签: python django

https://django-tables2.readthedocs.org/en/latest/pages/tutorial.html

我遵循了这个教程,并设法让表格显示但是它没有加载CSS文件。而不是这个(在HTML文件中指定):

<link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />

它只显示:

<link rel="stylesheet" href="django_tables2/themes/paleblue/css/screen.css" />

这是我的观点文件:

from django.shortcuts import render

# Create your views here.
from django.template import RequestContext
from django_tables2   import RequestConfig
from keywords.models  import Person
from keywords.tables  import PersonTable

def people(request):
    table = PersonTable(Person.objects.all())
    RequestConfig(request).configure(table)
    return render(request, 'people.html', {'table': table})

知道我缺少什么吗?

编辑:如果我在mysite / urls中添加它,那么它会在源代码中加载css,但会显示:&#34;一元+的错误操作数类型:&#39; list&#39;错误:

+ patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.MEDIA_ROOT,
}),

编辑2,尝试了一种不同的方式:

STATIC_ROOT = "/root/newproject/mysite/static_files/"

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static_files/"),    
)

STATIC_URL = '/static/'

这是设置,我用它来调用它:

<link href="{% static 'screen.css' %}" rel="stylesheet"/>

它没有做任何事情,但如果我将其更改为:

{{% static 'screen.css' %}}

然后加载css但是出现此错误:

/ keywords /中的

TemplateSyntaxError 无法解析余数:&#39;%static&#39; screen.css&#39; %&#39;来自&#39;%static&#39; screen.css&#39; %&#39;

2 个答案:

答案 0 :(得分:2)

我使用了Ubuntu,文件screen.css在

/usr/lib/python2.7/dist-packages/django_tables2/static/django_tables2/themes/paleblue/css/screen.css

然后,

我改变了

    <link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />

    <link rel="stylesheet" href="/static/django_tables2/themes/paleblue/css/screen.css" />

它对我有用。

抱歉我的英文

答案 1 :(得分:0)

您在使用此变量之前是否{% load staticfiles %}了?