I am very new to django
, and gone through tutorial for many days , i have started building a small website using django and trying to serve a css
file by arranging all the necessary settings in settings.py
file. But unfortunately my code is unable to serve the css file, i mean the concept of serving css files is not working. I googled a lot and gone through the django main doc tutorials and made changes according to them,and still doesn't works so approached SO
and pasted my entire code below
我是django的新手,并且经历了很多天的教程,我已经开始使用django构建一个小型网站,并尝试通过在settings.py文件中安排所有必要的设置来提供css文件。但不幸的是我的代码无法提供css文件,我的意思是服务css文件的概念不起作用。我google了很多,并通过django主要文档教程并根据它们进行了更改,但仍然没有工作所以接近SO并将我的整个代码粘贴到下面
Structure of project folder
项目文件夹的结构
personnel_blog
|____personnel_blog
|____manage.py |
|____media
|____static
| |____css
| |____personnel_blog_hm.css
|____template
| |____home_page.html
|____settings.py
|____urls.py
|____views.py
|____wsgi.py
Some of my settings.py file settings are below
我的一些settings.py文件设置如下
settings.py
settings.py
import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'static'),
# 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.
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR,'templates')
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
urls.py
urls.py
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'personnel_blog.views.home_page'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True }),
)
)
views.py
views.py
from django.shortcuts import render_to_response
def home_page(request):
return render_to_response("home_page.html")
home_page.html
home_page.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="{{ STATIC_URL }}css/personnel_blog_hm.css" type="text/css">
</head>
<body>
<p>Hello !</p>
<a href="/" target="_top">Home</a>
</body>
</html>
personnel_blog_hm.css
personnel_blog_hm.css
body { background-color:green; }
p {color:blue;background-color:green;padding-left:20px;}
So above is my code, can anyone please let me know whats wrong in the settigns.py file or other py files ?
所以上面是我的代码,任何人都可以让我知道settigns.py文件或其他py文件中的错误?
Whether need to do any other additional settings in the above code ?
是否需要在上面的代码中进行任何其他附加设置?
so can anyone please adjust my code and make necessary changes so that i cam move forward and make my first step in web designing ..... :)
所以任何人都可以调整我的代码并进行必要的更改,以便我向前推进并在网页设计中迈出第一步..... :)
4 个解决方案
#1
7
base.html
base.html文件
{% load static %}
<link rel="stylesheet" href="{% static 'css/personnel_blog_hm.css' %}" type="text/css">
settings
设置
PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
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_DIR, 'staticfiles'),
)
url
网址
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'personnel_blog.views.home_page'),
url(r'^admin/', include(admin.site.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
#2
0
Try changing your STATICFILES_DIRS
setting to
尝试将STATICFILES_DIRS设置更改为
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'static'),
)
#3
0
Your problem is related to this line:
您的问题与此行有关:
return render_to_response("home_page.html")
Django's template engine requires two things to properly render a template.
Django的模板引擎需要两件事才能正确呈现模板。
- The template name
- 模板名称
- A context variable
- 上下文变量
The context variable
is a key/value dictionary listing all of the variables available to the template.
上下文变量是键/值字典,列出了模板可用的所有变量。
The render_to_response
shortcut actually accepts two different context variable parameters.
render_to_response快捷方式实际上接受两个不同的上下文变量参数。
You're missing both.
你错过了两个。
Without these variables, the template doesn't have ANY variables available to it. So your {{ STATIC_URL }}
template variable is probably blank.
没有这些变量,模板就没有可用的任何变量。所以你的{{STATIC_URL}}模板变量可能是空白的。
To correct, try this:
要更正,请尝试以下方法:
from django.shortcuts import render_to_response
from django.template import RequestContext
def home_page(request):
return render_to_response("home_page.html", {}, context_instance=RequestContext(request))
#4
0
in settings.py
try.
在settings.py中尝试。
PROJECT_DIR = os.path.dirname(__file__)
...
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
my settings.py
file is usually in same directory as static directory
我的settings.py文件通常与静态目录位于同一目录中
#1
7
base.html
base.html文件
{% load static %}
<link rel="stylesheet" href="{% static 'css/personnel_blog_hm.css' %}" type="text/css">
settings
设置
PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
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_DIR, 'staticfiles'),
)
url
网址
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'personnel_blog.views.home_page'),
url(r'^admin/', include(admin.site.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
#2
0
Try changing your STATICFILES_DIRS
setting to
尝试将STATICFILES_DIRS设置更改为
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'static'),
)
#3
0
Your problem is related to this line:
您的问题与此行有关:
return render_to_response("home_page.html")
Django's template engine requires two things to properly render a template.
Django的模板引擎需要两件事才能正确呈现模板。
- The template name
- 模板名称
- A context variable
- 上下文变量
The context variable
is a key/value dictionary listing all of the variables available to the template.
上下文变量是键/值字典,列出了模板可用的所有变量。
The render_to_response
shortcut actually accepts two different context variable parameters.
render_to_response快捷方式实际上接受两个不同的上下文变量参数。
You're missing both.
你错过了两个。
Without these variables, the template doesn't have ANY variables available to it. So your {{ STATIC_URL }}
template variable is probably blank.
没有这些变量,模板就没有可用的任何变量。所以你的{{STATIC_URL}}模板变量可能是空白的。
To correct, try this:
要更正,请尝试以下方法:
from django.shortcuts import render_to_response
from django.template import RequestContext
def home_page(request):
return render_to_response("home_page.html", {}, context_instance=RequestContext(request))
#4
0
in settings.py
try.
在settings.py中尝试。
PROJECT_DIR = os.path.dirname(__file__)
...
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
my settings.py
file is usually in same directory as static directory
我的settings.py文件通常与静态目录位于同一目录中