我是django
的新手,并且经历了很多天的教程,我已经开始使用django构建一个小型网站,并尝试通过在{中安排所有必要的设置来提供css
文件{1}}文件。但不幸的是我的代码无法提供css文件,我的意思是服务css文件的概念不起作用。我搜索了很多内容并浏览了django主要文档教程并根据它们进行了更改,但仍然无效,所以接近settings.py
并将我的整个代码粘贴到下面
项目文件夹的结构
SO
我的一些settings.py文件设置在
之下settings.py
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
urls.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',
)
)
views.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 }),
home_page.html
from django.shortcuts import render_to_response
def home_page(request):
return render_to_response("home_page.html")
personnel_blog_hm.css
<!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>
以上是我的代码,任何人都可以告诉我settigns.py文件或其他py文件中的错误吗?
是否需要在上面的代码中进行任何其他附加设置?
所以任何人都可以调整我的代码并进行必要的更改,以便我向前推进并在网页设计中迈出第一步........
答案 0 :(得分:9)
<强> base.html文件强>
{% load static %}
<link rel="stylesheet" href="{% static 'css/personnel_blog_hm.css' %}" type="text/css">
设置强>
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'),
)
<强>网址
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()
答案 1 :(得分:0)
在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/'
我的settings.py
文件通常与静态目录
答案 2 :(得分:0)
尝试将STATICFILES_DIRS
设置更改为
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'static'),
)
答案 3 :(得分:0)
您的问题与此行有关:
return render_to_response("home_page.html")
Django的模板引擎需要两件事才能正确呈现模板。
context variable
是一个键/值字典,列出了模板可用的所有变量。
render_to_response
快捷方式实际上接受two different context variable parameters。
你错过了两个。
如果没有这些变量,模板就没有可用的任何变量。因此,您的{{ STATIC_URL }}
模板变量可能为空。
要更正,请尝试以下操作:
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))