之前讲过django的一些个东西,所以就不详细讲解了。主要说一下django是真怎使用已有数据库的(之前都是建立一个新的),还就是静态文件的问题,(setting部分)。

  先把setting贴上来:

View Code
  1 # Django settings for CatchShow project.
2 import os.path
3
4 DEBUG = True
5 TEMPLATE_DEBUG = DEBUG
6
7 ADMINS = (
8 # ('Your Name', 'your_email@example.com'),
9 )
10
11 HERE = os.path.abspath(os.path.dirname(__file__))
12
13 MANAGERS = ADMINS
14
15 DATABASES = {
16 'default': {
17 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
18 # 'NAME': 'Oj', # Or path to database file if using sqlite3.
19 'NAME': 'test',
20 'USER': 'root', # Not used with sqlite3.
21 'PASSWORD': '123', # 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 = '/media/'
58 #MEDIA_URL = ''
59
60 # Absolute path to the directory static files should be collected to.
61 # Don't put anything in this directory yourself; store your static files
62 # in apps' "static/" subdirectories and in STATICFILES_DIRS.
63 # Example: "/home/media/media.lawrence.com/static/"
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 STATIC_URL = '/images/'
70
71 # URL prefix for admin static files -- CSS, JavaScript and images.
72 # Make sure to use a trailing slash.
73 # Examples: "http://foo.com/static/admin/", "/static/admin/".
74 ADMIN_MEDIA_PREFIX = '/static/admin/'
75
76 # Additional locations of static files
77 STATICFILES_DIRS = (
78 ("hdoj","/home/duoduo/images/hdoj"),
79 "/home/duoduo/images",
80 #"/home/duoduo/data/images/hdoj",
81 # Put strings here, like "/home/html/static" or "C:/www/django/static".
82 # Always use forward slashes, even on Windows.
83 # Don't forget to use absolute paths, not relative paths.
84 )
85
86 # List of finder classes that know how to find static files in
87 # various locations.
88 STATICFILES_FINDERS = (
89 'django.contrib.staticfiles.finders.FileSystemFinder',
90 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
91 # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
92 )
93
94 # Make this unique, and don't share it with anybody.
95 SECRET_KEY = 'j#c+cf(ufoc^!)7ozqxlibwm218n1&%8bl0-utx7i$tlry$_6v'
96
97 # List of callables that know how to import templates from various sources.
98 TEMPLATE_LOADERS = (
99 'django.template.loaders.filesystem.Loader',
100 'django.template.loaders.app_directories.Loader',
101 # 'django.template.loaders.eggs.Loader',
102 )
103
104 MIDDLEWARE_CLASSES = (
105 'django.middleware.common.CommonMiddleware',
106 'django.contrib.sessions.middleware.SessionMiddleware',
107 'django.middleware.csrf.CsrfViewMiddleware',
108 'django.contrib.auth.middleware.AuthenticationMiddleware',
109 'django.contrib.messages.middleware.MessageMiddleware',
110 )
111
112 ROOT_URLCONF = 'CatchShow.urls'
113
114 TEMPLATE_DIRS = (
115 os.path.join(HERE, 'templates').replace('\\','/'),
116 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
117 # Always use forward slashes, even on Windows.
118 # Don't forget to use absolute paths, not relative paths.
119 )
120
121 INSTALLED_APPS = (
122 'django.contrib.auth',
123 'django.contrib.contenttypes',
124 'django.contrib.sessions',
125 'django.contrib.sites',
126 'django.contrib.messages',
127 'django.contrib.staticfiles',
128 # Uncomment the next line to enable the admin:
129 'django.contrib.admin',
130 # Uncomment the next line to enable admin documentation:
131 # 'django.contrib.admindocs',
132 'hdoj',
133 'toj',
134 )
135
136 # A sample logging configuration. The only tangible logging
137 # performed by this configuration is to send an email to
138 # the site admins on every HTTP 500 error.
139 # See http://docs.djangoproject.com/en/dev/topics/logging for
140 # more details on how to customize your logging configuration.
141 LOGGING = {
142 'version': 1,
143 'disable_existing_loggers': False,
144 'handlers': {
145 'mail_admins': {
146 'level': 'ERROR',
147 'class': 'django.utils.log.AdminEmailHandler'
148 }
149 },
150 'loggers': {
151 'django.request': {
152 'handlers': ['mail_admins'],
153 'level': 'ERROR',
154 'propagate': True,
155 },
156 }
157 }

  先说使用数据库部分,在可以看到,我数据库的NAME = 'test',把路径切换到test下面 python manage.py help 回发现里面有一堆的命令,这次需要用的那就是这三个:inspectdb(使用现有数据库)、collectstatic(把静态文件存到设置的static文件下)。

  好的,显示inspectdb,上图先:

  额,出现来了这么咄admin的类,这几行无大有所为,因为我启用来admin界面管理,它在我数据库里面加了一堆表,原来只有hdoj的时候只有这一个。

  然后把这个Hodj的class粘到hdoj的models.py里面去就ok了,已有数据库就能用了,比较容易。

  接着是静态文件,STATIC_URL = '/images/',说明模板里面只有有地方叫做/images/,django就会去查找你静态文件的路径去引用的想用到的文件。而这STATICFILES_DIRS = ( ("hdoj","/home/duoduo/images/hdoj"),"/home/duoduo/images",)个就是你设置的静态文件路径(本地);最后STATIC_ROOT = os.path.join(HERE,'static').replace('\\','/')就是你项目里面的静态文件。

  在用之前说的collectstatic,运行之后会是这个样子:

  点击yes后,会发现生成了一个static文件夹,static文件夹里面又有这些东西:

  解释:通过collectstatic之后,文件可以直接加到你的STATIC_ROOT下,而保存的方式就有点意思了,看:STATICFILES_DIRS,如果是一个元组,比如("hdoj","/home/duoduo/images/hdoj"),则把/home/duoduo/images/hdoj里的东西 存到static/hdoj/下面,如果是个字符串"/home/duoduo/images",那么就直接存到STATIC_ROOT根目录下,当然了,就算不执行collectstatic,依然可以引用静态文件。

  比如这张图:

  图片路径是<img src = '/images/hdoj/1056-1.gif'>,由于我们STATIC_URL = '/images/',那么django在看到这个前缀后就会查找/home/duoduo/images/hdoj/1056-1.gif,然后把它给显示出来。
  urls.py和之前一样,直接贴上来就可以了,最后贴一下吧:

View Code
 1 from django.conf.urls.defaults import patterns, include, url
2 from django.conf import settings
3 from django.contrib import admin
4 from CatchShow.hdoj import views as hdojViews
5 from CatchShow.toj import views as tojViews
6 from django.conf.urls.static import static
7 import os
8 admin.autodiscover()
9
10 urlpatterns = patterns('',
11 url(r'^$',hdojViews.homeIndex),
12 url(r'^hdoj/',hdojViews.index),
13 url(r'^toj/',tojViews.index),
14 url(r'^admin/', include(admin.site.urls)),
15 url(r'^showhdojproblemid=(?P<id>\d{4})$',hdojViews.showProblem),
16 url(r'^showtojproblemid=(?P<id>\d{4})$',tojViews.showProblem),
17 url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT },name='media'),
18 url(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT },name='static'),
19
20 )



  这就是通过这次作业的一些记录,因为之前查找网上资料狠不明了,所以才想写一下,希望能帮助更多蛇友们,那句话说的好,let's good good study,day day up~

posted on 2012-03-31 15:43  duoduo3_69  阅读(1848)  评论(2编辑  收藏  举报