之前创建了项目,编写的借阅者的一些操作。现在尝试着添加主页,通过浏览器展示这些操作。
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”,代码如下:
-
-
- ''
-
-
-
-
- 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”,代码如下:
-
-
-
- ''
-
-
-
-
-
- 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”,代码如下:
- "font-size:18px;">
-
- from django import template
- register = template.Library()
-
- @register.filter(name='calculate')
- def calculate(value, arg):
- return (int(arg)-1)*10 + int(value)
-
-
此过滤器用于页面展示的“序号”能够在翻页后依然依序递增。页面引用如下:
其中,“currentPage”为当前页码。
3. 设置“library” 的“setting”和“urls”
修改“library/library/setting.py”如下:
- "font-size:18px;">
-
- import os.path
-
- DEBUG = True
- TEMPLATE_DEBUG = DEBUG
- HERE = os.path.dirname(os.path.dirname(__file__))
-
- ADMINS = (
-
- )
-
- MANAGERS = ADMINS
-
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql',
- 'NAME': 'library',
- 'USER': 'root',
- 'PASSWORD': 'mysql',
- 'HOST': 'localhost',
- 'PORT': '3306',
- }
- }
-
-
-
-
-
-
-
-
- TIME_ZONE = 'Asia/Shanghai'
-
-
-
- LANGUAGE_CODE = 'zh-cn'
-
- SITE_ID = 1
-
-
-
- USE_I18N = True
-
-
-
- USE_L10N = True
-
-
- USE_TZ = True
-
-
-
- MEDIA_ROOT = os.path.join(HERE, 'media').replace('\\','/')
-
-
-
-
- MEDIA_URL = '/media/'
-
-
-
-
-
- STATIC_ROOT = os.path.join(HERE, 'static').replace('\\','/')
-
-
-
- STATIC_URL = '/static/'
-
- ADMIN_MEDIA_ROOT = '/static/admin/'
-
-
- STATICFILES_DIRS = (
-
-
-
- os.path.join(HERE,'static/static/').replace('\\','/'),
- )
-
-
-
- STATICFILES_FINDERS = (
- 'django.contrib.staticfiles.finders.FileSystemFinder',
- 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
-
- )
-
-
- SECRET_KEY = 'jlf4edatvxmm$*o)mfi=1cz_(vr21+32w(ak8_yac4*mxl8g%a'
-
-
- TEMPLATE_LOADERS = (
- 'django.template.loaders.filesystem.Loader',
- 'django.template.loaders.app_directories.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',
-
-
- )
-
- ROOT_URLCONF = 'library.urls'
-
-
- WSGI_APPLICATION = 'library.wsgi.application'
-
- TEMPLATE_DIRS = (
-
-
-
- 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',
-
-
-
-
- 'common',
- 'reader',
- )
-
-
-
-
-
-
- LOGGING = {
- 'version': 1,
- 'disable_existing_loggers': False,
- 'filters': {
- 'require_debug_false': {
- '()': 'django.utils.log.RequireDebugFalse'
- }
- },
- 'handlers': {
- 'mail_admins': {
- 'level': 'ERROR',
- 'filters': ['require_debug_false'],
- 'class': 'django.utils.log.AdminEmailHandler'
- }
- },
- 'loggers': {
- 'django.request': {
- 'handlers': ['mail_admins'],
- 'level': 'ERROR',
- 'propagate': True,
- },
- }
- }
-
- from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
- TEMPLATE_CONTEXT_PROCESSORS += (
- 'django.core.context_processors.request',
- )
-
修改“library/library/urls.py”如下:
- "font-size:18px;">from django.conf.urls import patterns, include, url
- from django.conf import settings
- from django.conf.urls.static import static
-
-
- from django.contrib import admin
- admin.autodiscover()
-
- urlpatterns = patterns('',
-
-
-
-
-
-
-
-
-
- url(r'^$', 'common.views.index',name="index"),
- url(r'^common/', include('common.urls')),
- url(r'^reader/', include('reader.urls')),
- )
-
- urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
- urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
4. 新建主页
简单修改dwz的主页信息。
右键文件夹“templates”,新建文件夹“common”。
右键文件夹“common”,新建“index.html”,代码如下:
- <span style="font-size:18px;">>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=7" />
- <title>图书管理系统title>
- {% load static %}
- <link href="{% get_static_prefix %}js/dwz/themes/default/style.css" rel="stylesheet" type="text/css" />
- <link href="{% get_static_prefix %}js/dwz/themes/css/core.css" rel="stylesheet" type="text/css" />
- <link href="{% get_static_prefix %}js/dwz/themes/css/print.css" rel="stylesheet" type="text/css" />
- <link href="{% get_static_prefix %}js/dwz/uploadify/css/uploadify.css" rel="stylesheet" type="text/css" />
-
- <script src="{% get_static_prefix %}js/dwz/javascripts/dwz.regional.zh.js" type="text/javascript">script>
-
-
- <script type="text/javascript">
- //标识dwz是否初始化
- dwzinit = true;
-
- function fleshVerify(){
- //重载验证码
- $('#verifyImg').attr("src", '__APP__/Public/verify/'+new Date().getTime());
- }
- function dialogAjaxMenu(json){
- dialogAjaxDone(json);
- if (json.statusCode == DWZ.statusCode.ok){
- //$("#sidebar").loadUrl("__APP__/Public/menu");
- }
- }
- function navTabAjaxMenu(json){
- navTabAjaxDone(json);
- if (json.statusCode == DWZ.statusCode.ok){
- //$("#sidebar").loadUrl("__APP__/Public/menu");
- }
- }
- $(function(){
- DWZ.init("{% get_static_prefix %}js/dwz/dwz.frag.xml", {
- loginUrl:"login_dialog.html", loginTitle:"登录", // 弹出登录对话框
- // loginUrl:"login.html", // 跳到登录页面
- statusCode:{ok:200, error:300, timeout:301}, //【可选】
- pageInfo:{pageNum:"pageNum", numPerPage:"numPerPage", orderField:"orderField", orderDirection:"orderDirection"}, //【可选】
- debug:false, // 调试模式 【true|false】
- callback:function(){
- initEnv();
- $("#themeList").theme({themeBase:"{% get_static_prefix %}js/dwz/themes"}); // themeBase 相对于index页面的主题base路径
- //$("#sidebar").loadUrl("__APP__/Public/menu");
- //initUI();
- }
- });
- });
-
-
- //清理浏览器内存,只对IE起效,FF不需要
- if ($.browser.msie) {
- window.setInterval("CollectGarbage();", 10000);
- }
-
-
- //新加处理ajax提交表单的处理方式
- jQuery(document).ajaxSend(function(event, xhr, settings) {
- function getCookie(name) {
- var cookieValue = null;
- if (document.cookie && document.cookie != '') {
- var cookies = document.cookie.split(';');
- for (var i = 0; i < cookies.length; i++) {
- var cookie = jQuery.trim(cookies[i]);
- // Does this cookie string begin with the name we want?
- if (cookie.substring(0, name.length + 1) == (name + '=')) {
- cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
- break;
- }
- }
- }
- return cookieValue;
- }
- function sameOrigin(url) {
- // url could be relative or scheme relative or absolute
- var host = document.location.host; // host + port
- var protocol = document.location.protocol;
- var sr_origin = '//' + host;
- var origin = protocol + sr_origin;
- // Allow absolute or scheme relative URLs to same origin
- return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
- (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
- // or any other URL that isn't scheme relative or absolute i.e relative.
- !(/^(\/\/|http:|https:).*/.test(url));
- }
- function safeMethod(method) {
- return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
- }
-
- if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
- xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
- }
- });
-
-
- script>
- head>
-
-
-
- <body scroll="no">
- <div id="layout">
- <div id="header">
- <div class="headerNav">
-
-
- <ul class="themeList" id="themeList">
- <li theme="default"><div class="selected">蓝色div>li>
- <li theme="green"><div>绿色div>li>
- ul>
- div>
-
-