Django:CSS不行 [英] Django: CSS Is not not working

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

问题描述

我现在还不到django,而且我的CSS工作有问题。

我已经遵循了链接的方向: Django Static Link教程,处理静态文件。但它仍然不起作用。

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.

# 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')


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

目录组织


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

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




非常感谢,您的帮助和时间非常感激!



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

推荐答案

对于Django提供静态文件,您必须确保有几个设置。

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

STATIC_URL

此设置指定静态文件映射到的URL。你已经完成了。

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

STATICFILES_DIRS

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

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

此设置指定Django将把所有的静态文件复制到静态文件的位置。这个想法是,一旦您将开发工作发展到生产中,Django无法提供静态文件,因为我不会去这里的问题(这是在文章)。但是,对于生产,所有静态文件应该在单个目录中,而不是像 STATICFILES_DIRS 中指定的那样。因此,此设置指定一个目录,Django将通过运行以下命令从 STATICFILES_DIRS 中的所有文件复制所有静态文件:

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

请请注意,只有在您投入生产后,这里所需的目录也不能与 STATICFILES_DIRS 中指定的任何目录相同。

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

在开发Django提供静态文件时,必须将静态网址包含在您的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()






一旦您完成上述所有操作,只要您有 DEBUG = True ,您的静态文件就可以运行。在上面的列表中,您似乎只完成 STATIC_URL 。另请注意,上述所有步骤都是您在问题中链接的文档(链接)。这可能在一开始就有点混乱,但如果你读了几遍,它会变得更加清晰。


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天全站免登陆