我很陌生django
,并且已经学习了很多天的教程,我已经开始使用 django 构建一个小型网站,并尝试css
通过在文件中安排所有必要的设置来提供settings.py
文件。但不幸的是,我的代码无法提供 css 文件,我的意思是提供 css 文件的概念不起作用。我用谷歌搜索了很多,并浏览了 django 主文档教程并根据它们进行了更改,但仍然无法正常工作,所以SO
我将整个代码粘贴在下面
项目文件夹结构
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
我的一些 settings.py 文件设置如下
设置.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',
)
网址.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 }),
)
视图.py
from django.shortcuts import render_to_response
def home_page(request):
return render_to_response("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>
person_blog_hm.css
body { background-color:green; }
p {color:blue;background-color:green;padding-left:20px;}
以上是我的代码,谁能告诉我 settigns.py 文件或其他 py 文件有什么问题吗?
是否需要在上面的代码中做任何其他额外的设置?
所以任何人都可以调整我的代码并进行必要的更改,以便我继续前进并迈出网页设计的第一步..... :)