Django:CSS 不起作用 [英] Django: CSS Is not not working

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

问题描述

我还是 django 的新手,我的 CSS 工作有问题.
我遵循了链接中的指示:Django 静态链接教程,关于处理静态文件.但它仍然无法正常工作.

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'

)

查看

#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 中包含静态 url:

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