在开发环境中提供django静态文件 [英] Serving django static files in development envirnoment

查看:170
本文介绍了在开发环境中提供django静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用静态文件加载我的css与runserver命令。问题是,我尝试所有解决方案,我发现这里stackoverflow和django文档,但它根本不工作...我不知道我该怎么办...
如果我设置

I want to use static files to load my css with runserver command. The problem is that i tried all the solution i found here on stackoverflow and also in django docs, but it doesnt works at all... I dont know what can i do... If i set

STATIC_URL = '/static/'
STATIC_ROOT = 'C:\Users\Max\Works\www\mysite\mysite\static'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

我以为足够了...我错过了什么?
你可以告诉我什么是最好的设置在静态环境中有静态文件?
感谢您的建议...

I thought it was enough... Am i missing something? Can you tell me what is the best setting for have static files in develompent envirnoment? Thanks in advice...

编辑(1)
我已经放入我的模板{{STATIC_URL}} css / dark-gray。 css,而css是C:\Users\Max\Works\www\mysite\mysite\static\css\dark-grey.css,我真的不能得到什么错误...

EDIT(1) I already putted in my template {{ STATIC_URL }}css/dark-grey.css" and ofc the css is in C:\Users\Max\Works\www\mysite\mysite\static\css\dark-grey.css, i really can't get what is wrong...

推荐答案

在您的settings.py

In your settings.py

调试= True

根据文档:


此视图会自动启用runserver(DEBUG设置
设置为True)。

This view is automatically enabled by runserver (with a DEBUG setting set to True).

使用URL模式是一种强制它,只要DEBUG = True,我个人甚至不需要做任何事情,在开发过程中总是会执行DEBUG,而当您切换到生产时,您甚至不使用开发服务器,所以您将将您的生产服务器指向静态位置。

Using the URL pattern is a way to force it, which I personally don't even have to do in my project as long as DEBUG=True. You would always have DEBUG on when you are developing, and when you switch to production you aren't even using the development server anyways, so you would be pointing your production server to the static location.

这是我的静态设置的代码片段y settings.py我不手动添加静态视图URL

This is a snippet of my static settings from my settings.py. I do not manually have to add that static view URL

import os

DEBUG = True

PROJECT_ROOT = os.path.dirname( __file__ )
PROJECT_NAME = os.path.basename(PROJECT_ROOT)

STATIC_ROOT = os.path.join(PROJECT_ROOT, '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.
    os.path.join(PROJECT_ROOT, 'web/'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)


TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.static',
    ...
    ...
)

这篇关于在开发环境中提供django静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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