Django:CSS不工作 [英] Django: CSS Is not not working

查看:169
本文介绍了Django:CSS不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是新的django,我有我的CSS工作的问题。

我按照从链接的方向: Django静态链接教程,关于处理静态文件。但它仍然不工作。



设置



 #绝对路径目录静态文件应该被收集到。 
#不要自己在这个目录中放任何东西;将静态文件
#存储在apps的static /子目录和STATICFILES_DIRS中。
#示例:/home/media/lawrence.com/static/
STATIC_ROOT ='/ Users / a9austin / Development / sites / AlphaSocks / src / static_root /'

#静态文件的URL前缀。
#示例:http://media.lawrence.com/static/
STATIC_URL ='/ static /'

#静态文件的其他位置
STATICFILES_DIRS =(
#将字符串放在这里,就像/ home / html / static或C:/ www / django / static。
# b#不要忘记使用绝对路径,而不是相对路径
'/ Users / a9austin / Development / sites / AlphaSocks / src / staticfiles'




查看



  #from django.http import HttpResponse 
from django.shortcuts import render_to_response


def index(request):
return render_to_response('index.html')






index.html



 < link rel =stylesheethref ={{STATIC_URL}} css / style.csstype =text / cssmedia =screen> 

和目录组织


src-> staticfiles-> css-> style.css





谢谢你,你的帮助和时间是非常感谢!

解决方案

为了让Django提供静态文件,您必须确保您有几个设置。



STATIC_URL



此设置指定静态文件映射到的URL。



这将指定所有文件夹你的系统,Django应该寻找静态文件。这个想法是,你的项目中可能有几个应用程序,每个应用程序可能需要一组不同的静态文件。因此,为了组织目的,每个应用程序可能包含一个 static 目录,它将只存储其静态文件。所以,Django必须有一个方法来知道这些目录是在哪里。这是此设置的用途。



STATIC_ROOT



Django将所有的静态文件复制到,而不是静态文件已经在那里。这个想法是,一旦你把开发放到生产中,Django不能再提供静态文件,因为我不会去这里的问题(它在 article )。但是对于生产,所有静态文件应该在一个目录中,而不是像 STATICFILES_DIRS 中指定的许多。因此,此设置指定一个目录,Django将通过运行以下命令从 STATICFILES_DIRS 中的所有文件复制所有静态文件:

  $ python manage.py collectstatic 



注意,这只有在您进入生产时才需要,并且此处指定的目录不能与 STATICFILES_DIRS 中指定的任何目录相同。



Url.py



在开发Django以提供静态文件时,您必须将静态网址urls.py:

  from django.contrib.staticfiles.urls import staticfiles_urlpatterns 

urlpatterns = .. 。

urlpatterns + = staticfiles_urlpatterns()






一旦你完成上述所有的事情,你的静态文件应该服务,只要你有 DEBUG = True 。在上面的列表中,您似乎只完成 STATIC_URL 。另请注意,上述所有步骤都在您在问题中链接的文档中(链接)。这可能是一个有点混乱的开始,但如果你读了几次,它变得更清楚。


I am still new to django and I am having problem with my CSS working.
I have followed the direction from the link: Django Static Link tutorial, on handling static files. But it is still not working.

Settings

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/Users/a9austin/Development/sites/AlphaSocks/src/static_root/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/Users/a9austin/Development/sites/AlphaSocks/src/staticfiles'

)

view

#from django.http import HttpResponse
from django.shortcuts import render_to_response


def index(request):
return render_to_response('index.html')


index.html

<link rel="stylesheet" href="{{STATIC_URL}}css/style.css" type="text/css" media="screen" >

And directory organization

src->staticfiles->css->style.css



Thank you so much, your help and time is much appreciated!

解决方案

For Django to serve static files, you have to make sure you have a couple of settings.

STATIC_URL

This setting specifies what url should static files map to under. You have that done already.

STATICFILES_DIRS

This specifies all the folders on your system where Django should look for static files. The idea is that you might have a couple of apps within your project, and each app might require a different set of static files. So for organizational purposes, each app might contain a static directory where it will store only it's static files. So then Django has to have a way to know where those directories are. This is what this setting is for.

STATIC_ROOT

This setting specifies where Django will copy all the static files to and not where the static files are already at. The idea is that once you leave development into production, Django can't serve static files anymore due to issues I will not go here (it's in the article). However for production, all static files should be in a single directory, instead of in many like specified in STATICFILES_DIRS. So this setting specifies a directory to which Django will copy all the static files from from all files within STATICFILES_DIRS by running the following command:

$ python manage.py collectstatic

Please note this is only necessary once you go into production and also that the directory specified here cannot be the same as any directory specified in STATICFILES_DIRS.

Urls.py

In development for Django to serve your static files, you have to include the static urls in your urls.py:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = ...

urlpatterns += staticfiles_urlpatterns()


Once you will complete all of the above things, your static files should be serves as long as you have DEBUG = True. Out of the list above, you seem to only complete STATIC_URL. Also please note that all the steps I described above are in the docs you linked in your question (link). It might be a bit confusing in the beginning but if you read it a couple of times, it becomes clearer.

这篇关于Django:CSS不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆