之前创建了项目,编写的借阅者的一些操作。现在尝试着添加主页,通过浏览器展示这些操作。
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”,代码如下:
- #!usr/bin/env python
- #coding: utf-8
- '''''
- Created on 2012-8-3
- @author: jingwen.wu
- '''
- from django.shortcuts import render_to_response,get_object_or_404
- from django.template import RequestContext
- from django.http import HttpResponseRedirect
- from django.core.urlresolvers import reverse
- from django.contrib.auth.decorators import login_required
- def index(request):
- return render_to_response('common/index.html', context_instance=RequestContext(request))
- def nav_index(request):
- return render_to_response('common/nav_index.html', {}, context_instance=RequestContext(request))
- def nav_reader(request):
- return render_to_response('common/nav_reader.html', {}, context_instance=RequestContext(request))
2.2. 新建“urls.py”
右键包名“common”,新建“urls.py”,代码如下:
- #!usr/bin/env python
- #coding: utf-8
- '''''
- Created on 2012-8-3
- @author: jingwen.wu
- '''
- from django.conf.urls.defaults import *
- from django.conf import settings
- urlpatterns = patterns('',
- url(r'^$', 'common.views.index',name="index"),
- url(r'^nav_index/$', 'common.views.nav_index', name="common_index"),
- url(r'^nav_reader/$', 'common.views.nav_reader', name="common_reader"),
- )
2.3. 新建过滤器
右键包名“common”,新建包“templatetags”,右键“templatetags”,新建“common_tags.py”,代码如下:
- <span style="font-size:18px;">#!usr/bin/env python
- #coding: utf-8
- from django import template
- register = template.Library()
- @register.filter(name='calculate')
- def calculate(value, arg):
- return (int(arg)-1)*10 + int(value)
- </span>
其中,“currentPage”为当前页码。
3. 设置“library” 的“setting”和“urls”
修改“library/library/setting.py”如下:
- <span style="font-size:18px;"># Django settings for library project.
- import os.path
- DEBUG = True
- TEMPLATE_DEBUG = DEBUG
- HERE = os.path.dirname(os.path.dirname(__file__))
- ADMINS = (
- # ('Your Name', 'your_email@example.com'),
- )
- MANAGERS = ADMINS
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
- 'NAME': 'library', # Or path to database file if using sqlite3.
- 'USER': 'root', # Not used with sqlite3.
- 'PASSWORD': 'mysql', # Not used with sqlite3.
- 'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
- 'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
- }
- }
- # Local time zone for this installation. Choices can be found here:
- # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
- # although not all choices may be available on all operating systems.
- # On Unix systems, a value of None will cause Django to use the same
- # timezone as the operating system.
- # If running in a Windows environment this must be set to the same as your
- # system time zone.
- TIME_ZONE = 'Asia/Shanghai'
- # Language code for this installation. All choices can be found here:
- # http://www.i18nguy.com/unicode/language-identifiers.html
- LANGUAGE_CODE = 'zh-cn'
- SITE_ID = 1
- # If you set this to False, Django will make some optimizations so as not
- # to load the internationalization machinery.
- USE_I18N = True
- # If you set this to False, Django will not format dates, numbers and
- # calendars according to the current locale.
- USE_L10N = True
- # If you set this to False, Django will not use timezone-aware datetimes.
- USE_TZ = True
- # Absolute filesystem path to the directory that will hold user-uploaded files.
- # Example: "/home/media/media.lawrence.com/media/"
- MEDIA_ROOT = os.path.join(HERE, 'media').replace('\\','/')
- # URL that handles the media served from MEDIA_ROOT. Make sure to use a
- # trailing slash.
- # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
- MEDIA_URL = '/media/'
- # Absolute path to the directory static files should be collected to.
- # Don't put anything in this directory yourself; store your static files
- # in apps' "static/" subdirectories and in STATICFILES_DIRS.
- # Example: "/home/media/media.lawrence.com/static/"
- STATIC_ROOT = os.path.join(HERE, 'static').replace('\\','/')
- # URL prefix for static files.
- # Example: "http://media.lawrence.com/static/"
- STATIC_URL = '/static/'
- ADMIN_MEDIA_ROOT = '/static/admin/'
- # Additional locations of static files
- STATICFILES_DIRS = (
- # 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.
- os.path.join(HERE,'static/static/').replace('\\','/'),
- )
- # List of finder classes that know how to find static files in
- # various locations.
- STATICFILES_FINDERS = (
- 'django.contrib.staticfiles.finders.FileSystemFinder',
- 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
- # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
- )
- # Make this unique, and don't share it with anybody.
- SECRET_KEY = 'jlf4edatvxmm$*o)mfi=1cz_(vr21+32w(ak8_yac4*mxl8g%a'
- # List of callables that know how to import templates from various sources.
- TEMPLATE_LOADERS = (
- 'django.template.loaders.filesystem.Loader',
- 'django.template.loaders.app_directories.Loader',
- # 'django.template.loaders.eggs.Loader',
- )
- MIDDLEWARE_CLASSES = (
- 'django.middleware.common.CommonMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
- 'django.contrib.messages.middleware.MessageMiddleware',
- # Uncomment the next line for simple clickjacking protection:
- # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
- )
- ROOT_URLCONF = 'library.urls'
- # Python dotted path to the WSGI application used by Django's runserver.
- WSGI_APPLICATION = 'library.wsgi.application'
- TEMPLATE_DIRS = (
- # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
- # Always use forward slashes, even on Windows.
- # Don't forget to use absolute paths, not relative paths.
- os.path.join(HERE,'templates'),
- )
- INSTALLED_APPS = (
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.sites',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- # Uncomment the next line to enable the admin:
- # 'django.contrib.admin',
- # Uncomment the next line to enable admin documentation: