本文介绍了django {{STATIC_URL}}有时无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个可怜的django初学者在某些视图上访问静态文件(css,js)时遇到了阻止程序问题.主要是,在主页上可以完全访问那些静态文件,但是在其他页面上则不能,并且布局完全被破坏了.

A pathetic django beginner has got a blocker problem to access static files (css, js) on some views. Mainly, on the homepage those static files are perfectly accessible, but on a different page, it doesn't and the layout is totally broken.

这是我的设置.py:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'bands',
    'lyrics',
    'articles',
)
PROJECT_DIR = os.path.dirname(__file__)
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, "static"),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

这是我的基本通用模板的一部分,其视图得以扩展:

This is part of my base generic template which views extend:

<link rel="Shortcut icon" href="{{ STATIC_URL }}img/favicon32.png" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/bootstrap.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}js/sortable.js"></script>

渲染时,它返回这样的代码(这就是我想要的):

When rendered, it return such code (and this is what I want):

<link rel="Shortcut icon" href="/static/img/favicon32.png" />
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="/static/js/sortable.js"></script>

这是主页视图的代码(静态文件可以使用的代码):

This is the code of the homepage view (the one that static files work):

def slider(request):
    context = Context ({ 'articles': Article.objects.order_by('-created_at')[:5] })
    return render(request, 'articles/slider.html', context)

这是不访问静态文件的视图(它使用django文档上的分页示例):

and this is the view that doesn't access static files (it uses pagination example found on django docs):

def archive(request, page="1"):
    articles_list = Article.objects.all().filter(active=True)
    paginator = Paginator(articles_list, 6)
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)
    context = Context({
        'articles': articles
    })
    return render_to_response('articles/archive.html', context)

如果有人猜测可能出什么问题,请告诉我.

If anyone has a guess what might be wrong, please let me know.

当前项目的代码可作为 github存储库获得.

The code of current project is available as a github repo.

推荐答案

您必须使用 RequestContext :

You must use the RequestContext:

from django.template import RequestContext

def archive(request, page="1"):
    # ...
    return render_to_response('articles/archive.html',
                              context,
                              context_instance=RequestContext(request))

或使用 render 快捷方式,类似于 render_to_response ,但使用 RequestContext :

from django.shortcuts import render

def archive(request, page="1"):
    # ...
    return render('articles/archive.html', context)

请注意,您在 slider 视图中使用了 render .

Note you used render in your slider view.

这篇关于django {{STATIC_URL}}有时无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:58