Django 一个简单的图书管理程序 (三 展示)

之前创建了项目,编写的借阅者的一些操作。现在尝试着添加主页,通过浏览器展示这些操作。

1. 添加网页展示的信息

右键项目名称“library”,新建包“static”。

右键包名“static”,新建文件夹“static”。

在文件夹“static”中添加网页展示风格的一些信息。也可以在http://code.google.com/p/dwz/downloads/list下载,使用dwz的一些风格。


2. 新建对主页的操作

右键项目名称“library”,新建包“common”。

2.1. 新建“views.py”

右键包名“common”,新建“views.py”,代码如下:

[python]  view plain copy
  1. #!usr/bin/env python  
  2. #coding: utf-8  
  3. ''''' 
  4. Created on 2012-8-3 
  5.  
  6. @author: jingwen.wu 
  7. '''  
  8. from django.shortcuts import render_to_response,get_object_or_404  
  9. from django.template import RequestContext  
  10. from django.http import HttpResponseRedirect  
  11. from django.core.urlresolvers import reverse  
  12. from django.contrib.auth.decorators import login_required  
  13.   
  14. def index(request):  
  15.     return render_to_response('common/index.html', context_instance=RequestContext(request))  
  16.   
  17. def nav_index(request):  
  18.     return render_to_response('common/nav_index.html', {}, context_instance=RequestContext(request))  
  19.   
  20. def nav_reader(request):  
  21.     return render_to_response('common/nav_reader.html', {}, context_instance=RequestContext(request))  


2.2. 新建“urls.py”

右键包名“common”,新建“urls.py”,代码如下:

[python]  view plain copy
  1. #!usr/bin/env python  
  2. #coding: utf-8  
  3.   
  4. ''''' 
  5. Created on 2012-8-3 
  6.  
  7. @author: jingwen.wu 
  8. '''  
  9.   
  10. from django.conf.urls.defaults import *  
  11. from django.conf import settings  
  12.   
  13. urlpatterns = patterns('',  
  14.     url(r'^$''common.views.index',name="index"),  
  15.     url(r'^nav_index/$''common.views.nav_index', name="common_index"),  
  16.     url(r'^nav_reader/$''common.views.nav_reader', name="common_reader"),  
  17. )  


2.3. 新建过滤器

右键包名“common”,新建包“templatetags”,右键“templatetags”,新建“common_tags.py”,代码如下:

[python]  view plain copy
  1. <span style="font-size:18px;">#!usr/bin/env python  
  2. #coding: utf-8  
  3. from django import template  
  4. register = template.Library()  
  5.  
  6. @register.filter(name='calculate')  
  7. def calculate(value, arg):  
  8.     return (int(arg)-1)*10 + int(value)  
  9.   
  10. </span>  
此过滤器用于页面展示的“序号”能够在翻页后依然依序递增。页面引用如下:


其中,“currentPage”为当前页码。

3. 设置“library” 的“setting”和“urls”

修改“library/library/setting.py”如下:

[python]  view plain copy
  1. <span style="font-size:18px;"># Django settings for library project.  
  2.   
  3. import os.path  
  4.   
  5. DEBUG = True  
  6. TEMPLATE_DEBUG = DEBUG  
  7. HERE = os.path.dirname(os.path.dirname(__file__))  
  8.   
  9. ADMINS = (  
  10.     # ('Your Name', '[email protected]'),  
  11. )  
  12.   
  13. MANAGERS = ADMINS  
  14.   
  15. DATABASES = {  
  16.     'default': {  
  17.         'ENGINE''django.db.backends.mysql'# Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.  
  18.         'NAME''library',                      # Or path to database file if using sqlite3.  
  19.         'USER''root',                      # Not used with sqlite3.  
  20.         'PASSWORD''mysql',                  # Not used with sqlite3.  
  21.         'HOST''localhost',                      # Set to empty string for localhost. Not used with sqlite3.  
  22.         'PORT''3306',                      # Set to empty string for default. Not used with sqlite3.  
  23.     }  
  24. }  
  25.   
  26. # Local time zone for this installation. Choices can be found here:  
  27. # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name  
  28. # although not all choices may be available on all operating systems.  
  29. # On Unix systems, a value of None will cause Django to use the same  
  30. # timezone as the operating system.  
  31. # If running in a Windows environment this must be set to the same as your  
  32. # system time zone.  
  33. TIME_ZONE = 'Asia/Shanghai'  
  34.   
  35. # Language code for this installation. All choices can be found here:  
  36. # http://www.i18nguy.com/unicode/language-identifiers.html  
  37. LANGUAGE_CODE = 'zh-cn'  
  38.   
  39. SITE_ID = 1  
  40.   
  41. # If you set this to False, Django will make some optimizations so as not  
  42. # to load the internationalization machinery.  
  43. USE_I18N = True  
  44.   
  45. # If you set this to False, Django will not format dates, numbers and  
  46. # calendars according to the current locale.  
  47. USE_L10N = True  
  48.   
  49. # If you set this to False, Django will not use timezone-aware datetimes.  
  50. USE_TZ = True  
  51.   
  52. # Absolute filesystem path to the directory that will hold user-uploaded files.  
  53. # Example: "/home/media/media.lawrence.com/media/"  
  54. MEDIA_ROOT = os.path.join(HERE, 'media').replace('\\','/')  
  55.   
  56. # URL that handles the media served from MEDIA_ROOT. Make sure to use a  
  57. # trailing slash.  
  58. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"  
  59. MEDIA_URL = '/media/'  
  60.   
  61. # Absolute path to the directory static files should be collected to.  
  62. # Don't put anything in this directory yourself; store your static files  
  63. # in apps' "static/" subdirectories and in STATICFILES_DIRS.  
  64. # Example: "/home/media/media.lawrence.com/static/"  
  65. STATIC_ROOT = os.path.join(HERE, 'static').replace('\\','/')  
  66.   
  67. # URL prefix for static files.  
  68. # Example: "http://media.lawrence.com/static/"  
  69. STATIC_URL = '/static/'  
  70.   
  71. ADMIN_MEDIA_ROOT = '/static/admin/'  
  72.   
  73. # Additional locations of static files  
  74. STATICFILES_DIRS = (  
  75.     # Put strings here, like "/home/html/static" or "C:/www/django/static".  
  76.     # Always use forward slashes, even on Windows.  
  77.     # Don't forget to use absolute paths, not relative paths.  
  78.     os.path.join(HERE,'static/static/').replace('\\','/'),  
  79. )  
  80.   
  81. # List of finder classes that know how to find static files in  
  82. # various locations.  
  83. STATICFILES_FINDERS = (  
  84.     'django.contrib.staticfiles.finders.FileSystemFinder',  
  85.     'django.contrib.staticfiles.finders.AppDirectoriesFinder',  
  86. #    'django.contrib.staticfiles.finders.DefaultStorageFinder',  
  87. )  
  88.   
  89. # Make this unique, and don't share it with anybody.  
  90. SECRET_KEY = 'jlf4edatvxmm$*o)mfi=1cz_(vr21+32w(ak8_yac4*mxl8g%a'  
  91.   
  92. # List of callables that know how to import templates from various sources.  
  93. TEMPLATE_LOADERS = (  
  94.     'django.template.loaders.filesystem.Loader',  
  95.     'django.template.loaders.app_directories.Loader',  
  96. #     'django.template.loaders.eggs.Loader',  
  97. )  
  98.   
  99. MIDDLEWARE_CLASSES = (  
  100.     'django.middleware.common.CommonMiddleware',  
  101.     'django.contrib.sessions.middleware.SessionMiddleware',  
  102.     'django.middleware.csrf.CsrfViewMiddleware',  
  103.     'django.contrib.auth.middleware.AuthenticationMiddleware',  
  104.     'django.contrib.messages.middleware.MessageMiddleware',  
  105.     # Uncomment the next line for simple clickjacking protection:  
  106.     # 'django.middleware.clickjacking.XFrameOptionsMiddleware',  
  107. )  
  108.   
  109. ROOT_URLCONF = 'library.urls'  
  110.   
  111. # Python dotted path to the WSGI application used by Django's runserver.  
  112. WSGI_APPLICATION = 'library.wsgi.application'  
  113.   
  114. TEMPLATE_DIRS = (  
  115.     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  
  116.     # Always use forward slashes, even on Windows.  
  117.     # Don't forget to use absolute paths, not relative paths.  
  118.     os.path.join(HERE,'templates'),  
  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:  
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值