Django:GET css返回404? [英] Django: GET css returns 404?

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

问题描述

我正在开发一个Django网站,经过一系列编辑后,我突然运行我的服务器,没有css!服务器仍然显示html网站,当然,但是整个网站的css都抛出一个404错误。我的 settings.py 中的静态文件信息完全未编辑:

I am developing a Django site and after a series of edits I ran my server again when suddenly - no css! The server still displays the html site, of course, but the css all across the site is throwing a 404 error. My static files information in my settings.py wasn't edited at all:

import os
# hack to accommodate Windows
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__).decode('utf-8')).replace('\\', '/')

STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(CURRENT_PATH, 'static'),
)

简单调用基于静态文件目录的文件:

My base template does a simple call for the file based on the static file directory:

<link rel="Stylesheet" type="text/css" href="{{ STATIC_URL }}css.css" />

最后,当然,这里是我有关的目录结构的快速细分,因为我怀疑错误是某种目录问题:

And lastly, of course, here is a quick breakdown of my pertinent directory structure, since I suspect the error is some kind of directory issue:

-project-
     settings.py
     -static-
         css.css
     -templates-
         base.html
         index.html

我只提供了index.html的位置,但是templates文件夹当然填充了各种模板/页面的目录。

I've only provided the location of index.html, but the templates folder of course is filled with directories for various templates/pages.

推荐答案

有很多事情你需要做以便获得在开发平台上工作的静态网址。

There are a number of things you need to do in order to get the static urls working on the development platform.

对于这个,我建议浏览并阅读此链接。 https://docs.djangoproject.com/en/dev/howto/static-文件/

For this i would recommend looking through and reading this link. https://docs.djangoproject.com/en/dev/howto/static-files/

除此之外,您还需要从您的视图返回一个请求上下文。

As well as this you also need to return a request context from your view.

请注意,在您的urls.py文件中,您还需要添加行。

Note that within your urls.py file you will also need to add the line.

urlpatterns += staticfiles_urlpatterns()

此行允许开发服务器中的URL模块提供静态文件。

This line allows the URL's module within a development server to serve static files.

我也注意到你的 os.path.join 代码中有一个变量。 os.path.join 如果您正在使用基本的django设置,则已经指向您的开发目录。

I also notice that you have a variable within your os.path.join code. os.path.join will already be pointing at you development directory if you are using a basic django setup.

您应该能够使用:

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join('static'),
)

注意:是返回一个请求上下文,因为这是填充 STATIC_URL 变量所必需的。同样,请确保您拥有所有 TEMPLATE_CONTEXT_PROCESSORS 添加到 settings.py 文件

Note: Also make sure your views are a returning a request context, as this is required for the STATIC_URL variable to be populated. Likewise, ensure you have all the TEMPLATE_CONTEXT_PROCESSORS added into the settings.py file

这篇关于Django:GET css返回404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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