标签: django-staticfiles

Django没有加载CSS?

这是我的urls.py

import os.path
site_media = os.path.join(
    os.path.dirname(__file__), 'site_media'
)

urlpatterns = patterns('',
    url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
    { 'document_root': site_media }),
)
Run Code Online (Sandbox Code Playgroud)

我的site_media文件夹和style.css文件在

myProjectFolder/myApp/static/site_media/css/style.css
Run Code Online (Sandbox Code Playgroud)

在我的base.html模板中(我所有其他模板都从该base.html模板扩展而来),这就是我所拥有的:

<head>
<title>{% block title %}{% endblock %}</title>
<link rel='stylesheet' type='text/css' href='/site_media/css/style.css' />
</head>

<body>
    {% block header %}{% endblock %}

    {% block content %}
        <div id='header'></div>
    {% endblock %}
</body>
Run Code Online (Sandbox Code Playgroud)

在我的style.css中,我只有这样:

#header {
    display: inline;
    background-color: black;
    color: black;
    border: 1px solid green;
}
Run Code Online (Sandbox Code Playgroud)

并且CSS没有被应用到

#header
Run Code Online (Sandbox Code Playgroud)

div。知道为什么吗?

html css django django-templates django-staticfiles

3
推荐指数
1
解决办法
1万
查看次数

django静态文件未加载

我有一个问题,我按照教程https://docs.djangoproject.com/en/1.5/intro/tutorial06/中的说明进行了所有操作,并且所有操作都运行良好,但是CSS,图像未显示其效果。作为django上的新手,需要建议。谢谢你的帮助。

我的css文件:-

li a {
    color: red;
}
body {
    background: white url("images/background.gif") no-repeat right bottom;
}
Run Code Online (Sandbox Code Playgroud)

url.py文件:-

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import patterns,url
from polls import views
urlpatterns = patterns('',
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'), 
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Run Code Online (Sandbox Code Playgroud)

Index.html文件-

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static '/polls/style.css' %}"/>
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
    <li><a href="{% …
Run Code Online (Sandbox Code Playgroud)

django django-staticfiles

3
推荐指数
3
解决办法
2万
查看次数

pythonanywhere的django settings.py

我有 django 应用程序,我在本地开发并部署到 pythonanywhere。

本地 Setting.py 包含 sqlite-db、本地静态路径等。 pythonanywhere 上的 Setting.py 包含 mysql-db、静态的 cdn 链接等。

我想在我的 git 中有一个设置文件,有一些像这样的检查

if host_type=='pythonanywhere':
    DATABASES = { "default": { "ENGINE": "django.db.backends.mysql",
Run Code Online (Sandbox Code Playgroud)

为此,settings.py 的最佳实践在哪里?

pythonanywhere 提供了哪些 os.environ?

django-settings django-staticfiles pythonanywhere

3
推荐指数
1
解决办法
918
查看次数

Pythonanywhere'staticfiles'不是有效的标记库:找不到模板库staticfiles

在pythonanywhere中,我正在使用带有Django 1.7和Python 2.7的virtualenv

Settings.py

STATIC_ROOT = '/home/movies/pantherlist/movies/static/'
STATIC_URL = '/static/'

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pantherlist.movies',
)
Run Code Online (Sandbox Code Playgroud)

wsgi.py

activate_this = '/home/movies/.virtualenvs/django17/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
.
.#path setup already done here
.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Run Code Online (Sandbox Code Playgroud)

我收到了错误

异常类型:TemplateSyntaxError异常值:
'staticfiles'不是有效的标记库:找不到模板库staticfiles,尝试过django.templatetags.staticfiles,django.contrib.admin.templatetags.staticfiles

异常位置:/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py in load,1054行

index.html出错

{% load staticfiles %}
Run Code Online (Sandbox Code Playgroud)

请帮忙.提前致谢.

python django django-staticfiles pythonanywhere

3
推荐指数
1
解决办法
823
查看次数

如何在Django应用程序视图中引用静态文件

我知道这里有很多关于在Django项目中访问静态文件的问题,但是对于正在处理的问题,我找不到令人满意的答案。所以,这是我的项目结构:

.
??? project
?   ??? __init__.py
?   ??? settings.py
?   ??? urls.py
?   ??? wsgi.py
??? db.sqlite3
??? manage.py
??? app
    ??? admin.py
    ??? __init__.py
    ??? migrations
    ?   ??? __init__.py
    ??? models.py
    ??? static
    ?   ??? app
    ?       ??? file.txt
    ??? tests.py
    ??? urls.py
    ??? views.py
Run Code Online (Sandbox Code Playgroud)

根据Django 1.7文档,我已将静态文件file.txt存储在app / static / app文件夹中。现在,如何在视图中引用此文件?可以说,我需要为app / views.py中定义的每个调用读取此文件的内容。

和往常一样,我在project / settings.py中使用了STATIC_URL ='/ static /'的默认设置

谢谢

python django django-staticfiles

3
推荐指数
2
解决办法
4677
查看次数

Django:静态图片不会加载

我一直在关注这封信的官方文档,在这里尝试一些有关相关问题的建议,并且只是在网上进行搜索,而仍然无法运气加载一张小图片。

我有一个名为“ logo.png”的图像,我的项目层次结构如下所示:

project/
   mysite/
      app/
         __init__.py
         admin.py
         models.py
         urls.py
         view.py
      mysite/
         __init__.py
         settings.py
         urls.py
         views.py
         wsgi.py
      static/
         logo.png
      templates/
         index.html
         calling_image.html
Run Code Online (Sandbox Code Playgroud)

里面settings.py我有STATIC_URL = '/static/'

里面calling_image.html我有<img src="/static/logo.png">

我的模板calling_image.html由调用,project/mysite/app/views.py然后由调用project/mysite/app/urls.py,我什至尝试在我的末尾包括以下两行(如我所见几次)urls.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Run Code Online (Sandbox Code Playgroud)

什么都没有,所以我做错了什么?

编辑:抱歉,我输入了一个类型,我在STATIC_URL = '/static/'中间用了斜杠settings.py,为了澄清起见,我正在使用python manage.py runserver

解决了:所以我最终自己解决了这个问题。我创建了一个目录resourcesproject/mysite/,并放置logo.png在那里。然后我开始STATICFILES_DIRS = (os.path.join(BASE_DIR, "resources"),)跑步collecstatic,一切正常!不需要使用urlpatterns += staticfiles_urlpatterns(),没什么。

python django django-staticfiles

3
推荐指数
1
解决办法
7668
查看次数

Django静态js文件无法正常工作

好吧,我的模板代码.

<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" src="{% static 'website/staffaddproblem.js' %}"></script>
    <title>Dashboard</title>
    <link href="{% static 'website/style.css' %}" rel="stylesheet" type="text/css"/>
</head>
<body>
        body
</body>
<html>
Run Code Online (Sandbox Code Playgroud)

如您所见,它有一个css文件.哪个工作正常,但java脚本文件不起作用.当我加载页面时,它的源代码如下所示. 在此输入图像描述

当我点击链接时,它加载文件,它的链接很好,但它不起作用.代码js代码在同一文件中编写时没有问题.

javascript django django-templates django-staticfiles

3
推荐指数
1
解决办法
8860
查看次数

Django findstatic:找不到匹配的文件

前一段时间,我尝试在Django中使用“ collectstatic”来提供我的静态文件,但失败了。现在,我终于试图让我的静态文件在我的开发环境中正确运行。我不知道出了什么问题。

当我跑步 python manage.py findstatic images/add.png

控制台返回:

base path: C:\Projects\AlmondKing\AlmondKing
static files dirs: C:\Projects\AlmondKing\AlmondKing\static
No matching file found for 'images/add.png'.
Run Code Online (Sandbox Code Playgroud)

目录路径是正确的,但仍然找不到我的文件。我尝试了许多不同的设置配置,但均无济于事。谁能发现我的问题?我认为这是所有相关设置:

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

DEBUG = True

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'AlmondKing.InventoryLogs',
    'AlmondKing.FinancialLogs',
    'AlmondKing.AKGenius',
)

STATIC_URL = '/static/'
STATIC_FILES_DIRS = (
    os.path.join((BASE_DIR), "static")
)

print("base path:", BASE_DIR)
print("static files dirs:", STATIC_FILES_DIRS)
Run Code Online (Sandbox Code Playgroud)

编辑:我发现了,findstatic --verbosity 2 command并且正在搜索的唯一目录是:C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\contrib\admin\static

显然,它只是在我的虚拟环境中查找,而不是我的项目目录。这是正常的Django行为吗?

python django virtualenv django-settings django-staticfiles

3
推荐指数
1
解决办法
1155
查看次数

在Django中提供静态文件的最佳实践

在Django中为生产提供图像文件的最佳做法是什么?我想用静态图像回复,我正在将我的Django应用程序部署到Heroku.

使用django.middleware.security.SecurityMiddleware而不是whitenoise.middleware.WhiteNoiseMiddleware在效率或安全性方面有任何明显的缺点吗?

与使用whitenoise相比,下面的代码是否效率低下?提供settings.MEDIA_ROOT与提供静态文件相同的图像?

img =  os.path.join(settings.MEDIA_ROOT, filename)
try:
    with open(img, "rb") as f:
        return HttpResponse(f.read(), content_type="image/jpeg")
except IOError:
    failedResponse = '{"detail": "No image found"}'
    return HttpResponse(failedResponse)
Run Code Online (Sandbox Code Playgroud)

python django heroku django-middleware django-staticfiles

3
推荐指数
1
解决办法
1755
查看次数

无法在 Django 1.8 中解析静态 JSON 文件

我有以下树:

myDjangoSite/
    ...
    myApp/
        ...
        static/
            ...
            myApp/
                myData.json
Run Code Online (Sandbox Code Playgroud)

我有这个 view.py 文件:

import json
from django.shortcuts import render
from django.contrib.staticfiles.templatetags.staticfiles import static


url = static('myApp/myData.json')

json_data = open(url)

def helloWorld(request):

    return render(request, 'myApp/index.html')
Run Code Online (Sandbox Code Playgroud)

...当我访问网页时,浏览器会显示以下消息:

IOError at / [Errno 2] 没有这样的文件或目录:'/static/myApp/myData.json'

我不明白为什么它说“没有这样的文件或目录”,如果文件确实存在以及如何解决它。

python django django-views python-2.7 django-staticfiles

3
推荐指数
1
解决办法
1436
查看次数