设为首页 收藏本站
查看: 1343|回复: 0

[经验分享] 用python抓取oj题目(3)——django显示

[复制链接]

尚未签到

发表于 2015-4-20 09:39:10 | 显示全部楼层 |阅读模式
  之前讲过django的一些个东西,所以就不详细讲解了。主要说一下django是真怎使用已有数据库的(之前都是建立一个新的),还就是静态文件的问题,(setting部分)。
  先把setting贴上来:


DSC0000.gif DSC0001.gif 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,上图先:
DSC0002.png
  额,出现来了这么咄admin的类,这几行无大有所为,因为我启用来admin界面管理,它在我数据库里面加了一堆表,原来只有hdoj的时候只有这一个。
DSC0003.png
  然后把这个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,运行之后会是这个样子:
DSC0004.png
  点击yes后,会发现生成了一个static文件夹,static文件夹里面又有这些东西:
DSC0005.png
  解释:通过collectstatic之后,文件可以直接加到你的STATIC_ROOT下,而保存的方式就有点意思了,看:STATICFILES_DIRS,如果是一个元组,比如("hdoj","/home/duoduo/images/hdoj"),则把/home/duoduo/images/hdoj里的东西 存到static/hdoj/下面,如果是个字符串"/home/duoduo/images",那么就直接存到STATIC_ROOT根目录下,当然了,就算不执行collectstatic,依然可以引用静态文件。
  比如这张图:
DSC0006.png
  图片路径是,由于我们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\d{4})$',hdojViews.showProblem),
16     url(r'^showtojproblemid=(?P\d{4})$',tojViews.showProblem),
17     url(r'^media/(?P.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT },name='media'),
18     url(r'^static/(?P.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT },name='static'),
19         
20 )
  

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-58673-1-1.html 上篇帖子: Python中的几种数据类型 下篇帖子: python开发_json_一种轻量级的数据交换格式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表