站点管理和静态文件配置urls.py和settings.py

  django自带站点管理,挺好用的,添加数据的时候就不必要用mysql那些麻烦的命令了,另外如果在模版上内想使用图片、css之类的,还需要配置静态文件,开启这些东西需要在urls.py和settings.py里面做一些设置。先上代码。

  settings.py

  1 # Django settings for AddressBook project.
2 import os.path
3
4 DEBUG = True
5 TEMPLATE_DEBUG = DEBUG
6
7 HERE = os.path.abspath(os.path.dirname(__file__))
8
9
10 ADMINS = (
11 # ('Your Name', 'your_email@example.com'),
12 )
13
14 MANAGERS = ADMINS
15
16 DATABASES = {
17 'default': {
18 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
19 'NAME': 'AdressBook', # Or path to database file if using sqlite3.
20 'USER': 'root', # Not used with sqlite3.
21 'PASSWORD': 'duoduo', # Not used with sqlite3.
22 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
23 'PORT': '', # Set to empty string for default. Not used with sqlite3.
24 }
25 }
26
27 # Local time zone for this installation. Choices can be found here:
28 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
29 # although not all choices may be available on all operating systems.
30 # On Unix systems, a value of None will cause Django to use the same
31 # timezone as the operating system.
32 # If running in a Windows environment this must be set to the same as your
33 # system time zone.
34 TIME_ZONE = 'Asia/Shanghai'
35
36 # Language code for this installation. All choices can be found here:
37 # http://www.i18nguy.com/unicode/language-identifiers.html
38 LANGUAGE_CODE = 'zh-CN'
39
40 SITE_ID = 1
41
42 # If you set this to False, Django will make some optimizations so as not
43 # to load the internationalization machinery.
44 USE_I18N = True
45
46 # If you set this to False, Django will not format dates, numbers and
47 # calendars according to the current locale
48 USE_L10N = True
49
50 # Absolute filesystem path to the directory that will hold user-uploaded files.
51 # Example: "/home/media/media.lawrence.com/media/"
52 MEDIA_ROOT= os.path.join(HERE,'static')
53
54 # URL that handles the media served from MEDIA_ROOT. Make sure to use a
55 # trailing slash.
56 # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
57 MEDIA_URL = ''
58
59 # Absolute path to the directory static files should be collected to.
60 # Don't put anything in this directory yourself; store your static files
61 # in apps' "static/" subdirectories and in STATICFILES_DIRS.
62 # Example: "/home/media/media.lawrence.com/static/"
63 STATIC_ROOT = ''
64 #STATIC_ROOT = os.path.join(HERE,'static').replace('\\','/')
65
66 # URL prefix for static files.
67 # Example: "http://media.lawrence.com/static/"
68 STATIC_URL = '/static/'
69
70 # URL prefix for admin static files -- CSS, JavaScript and images.
71 # Make sure to use a trailing slash.
72 # Examples: "http://foo.com/static/admin/", "/static/admin/".
73 ADMIN_MEDIA_PREFIX = '/static/admin/'
74
75 # Additional locations of static files
76 STATICFILES_DIRS = (
77 # Put strings here, like "/home/html/static" or "C:/www/django/static".
78 # Always use forward slashes, even on Windows.
79 # Don't forget to use absolute paths, not relative paths.
80 )
81
82 # List of finder classes that know how to find static files in
83 # various locations.
84 STATICFILES_FINDERS = (
85 'django.contrib.staticfiles.finders.FileSystemFinder',
86 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
87 # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
88 )
89
90 # Make this unique, and don't share it with anybody.
91 SECRET_KEY = 'ryjl%indi789zuf!k8ihxsc6ku)$7**@)&i9olv-g0yf&5xa=m'
92
93 # List of callables that know how to import templates from various sources.
94 TEMPLATE_LOADERS = (
95 'django.template.loaders.filesystem.Loader',
96 'django.template.loaders.app_directories.Loader',
97 # 'django.template.loaders.eggs.Loader',
98 )
99
100 MIDDLEWARE_CLASSES = (
101 'django.middleware.common.CommonMiddleware',
102 'django.contrib.sessions.middleware.SessionMiddleware',
103 'django.middleware.locale.LocaleMiddleware',
104 'django.middleware.csrf.CsrfViewMiddleware',
105 'django.contrib.auth.middleware.AuthenticationMiddleware',
106 'django.contrib.messages.middleware.MessageMiddleware',
107 )
108
109 ROOT_URLCONF = 'AddressBook.urls'
110
111 TEMPLATE_DIRS = (
112 os.path.join(HERE, 'templates').replace('\\','/'),
113 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
114 # Always use forward slashes, even on Windows.
115 # Don't forget to use absolute paths, not relative paths.
116 )
117
118 INSTALLED_APPS = (
119 'django.contrib.auth',
120 'django.contrib.contenttypes',
121 'django.contrib.sessions',
122 'django.contrib.sites',
123 'django.contrib.messages',
124 'django.contrib.staticfiles',
125 # Uncomment the next line to enable the admin:
126 'django.contrib.admin',
127 # Uncomment the next line to enable admin documentation:
128 'django.contrib.admindocs',
129 # 'AddressBook.highschool',
130 'highschool',
131 )
132
133 # A sample logging configuration. The only tangible logging
134 # performed by this configuration is to send an email to
135 # the site admins on every HTTP 500 error.
136 # See http://docs.djangoproject.com/en/dev/topics/logging for
137 # more details on how to customize your logging configuration.
138 LOGGING = {
139 'version': 1,
140 'disable_existing_loggers': False,
141 'handlers': {
142 'mail_admins': {
143 'level': 'ERROR',
144 'class': 'django.utils.log.AdminEmailHandler'
145 }
146 },
147 'loggers': {
148 'django.request': {
149 'handlers': ['mail_admins'],
150 'level': 'ERROR',
151 'propagate': True,
152 },
153 }
154 }


  urls.py

 1 from django.conf.urls.defaults import patterns, include, url
2 from AddressBook.highschool import views
3 from django.contrib import admin
4 from django.conf import settings
5 admin.autodiscover()
6
7 urlpatterns = patterns('',
8 url(r'^$', views.search),
9 url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
10 url(r'^search/$', views.search),
11 url(r'^admin/', include(admin.site.urls)),
12 url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT }),
13 )


  首先是站点管理

  一共有3步

  1、urls.py中把第3行from django.contrib import admin和第5行admin.autodiscover()第11行url(r'^admin/', include(admin.site.urls))的注释去掉

  2、settings.py中注意125~128行,一样去掉注释

  3、命令行运行python manage.py syncdb

  现在在你的站点最后加上admin/的后缀就可以打开站点管理了(http://127.0.0.1:8080/admin/)

  静态文件配置

  先看下settings.py中需要说明的一些东西。

   2 import os.path

        7 HERE = os.path.abspath(os.path.dirname(__file__))  

  52 MEDIA_ROOT= os.path.join(HERE,'static')

 这三句是句是为了设置MEDIA_ROOT的绝对路径,第7行的函数就是让django探索你项目的绝对路径,而不必要写类似C:/XXX或者/home/XXX什么了

然后看url.py里面

4 from django.conf import settings

12 url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT }),

 这两行直接加上就行了

剩下两个步骤,以css为例
1、在根目录下建立一个static的文件夹,在再static文件夹下建立一个css文件夹,把你的样式表塞进去
2、在模版中<head>标签里面加上这一句<link type="text/css" href="/site_media/css/base.css" rel="stylesheet" />,来引用你的样式表

  大功告成,这里需要说明一下:

  可以看到在settings.py里面第63行有个STATIC_ROOT,照理说静态文件应该是用这个路径,为什么用MEDIA那,这个我也不了解,在网上好不容易找到几个静态文件配置的帖子,说的不明白,官档讲到也是很晕,地址https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/,然后是url.py里面'^site_media/这个东西,原来我找到帖子里面有这几种,media/,static/和site_media/,当然用的时候我在模版中href="/site_media/css/base.css这里都有相应改动,但不知道怎么回事儿,用前两个就是不行,当时纠结和1个多小时,后来随便改了个site_media就行了,貌似是用static和media之后与django的神马名字有冲突还是怎么滴,不太了解。

  配置完成之后,在模版就可以引用图片、样式表之类的了,表格显示就好多了,最后贴张图吧,标示html+css完全是用firebugs+w3c一起粘过来用的,有点丑。

  

  

posted on 2012-02-08 20:30  duoduo3_69  阅读(2055)  评论(1编辑  收藏  举报