根据 The Django Book 实施的,Django 操作 MySQL 完整案例 接收朋友的批评,终于给弄出来了,再次感谢! 环境 RHEL 6.1 X64 [root@gfs03 book]# python Python 2.6.6 (r266:84292, Apr 11 2011, 15:50:32) [GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. »> import django »> django.VERSION (1, 2, 5, ‘final’, 0) »> 创建项目
[root@gfs03 ~]# django-admin startproject book [root@gfs03 ~]# cd book/ [root@gfs03 book]# ls __init__.py manage.py settings.py urls.py [root@gfs03 book]# vi models.py [root@gfs03 book]# vi views.py [root@gfs03 book]# vi urls.py [root@gfs03 book]# vi latest_books.html
from django.shortcuts import render_to_response from models import Book def latest_books(request): book_list = Book.objects.order_by('-pub_date')[:10] return render_to_response('latest_books.html', {'book_list': book_list})
urls.py
Configuration File: /root/book/urls.py from django.conf.urls.defaults import * import views # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover()
DATABASES = { 'default': { 'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'book', # Or path to database file if using sqlite3. 'USER': 'root', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } }
# Local time zone for this installation. Choices can be found here: # # 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 = 'America/Chicago'
# Language code for this installation. All choices can be found here: # LANGUAGE_CODE = 'en-us'
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
# Absolute filesystem path to the directory that will hold user-uploaded files. # Example: "/home/media/media.lawrence.com/" MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash if there is a path component (optional in other cases). # Examples: "", "" MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a # trailing slash. # Examples: "", "/media/". ADMIN_MEDIA_PREFIX = '/media/'
# Make this unique, and don't share it with anybody. SECRET_KEY = 'sn^ontr8hjzrsnf$yz0eilo*r_0^3adsww-vsl9o5lgw9j90^8'
# 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', )
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. '/root/book', )
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', )
导入数据库
Configuration File: book.sql -- MySQL dump 10.13 Distrib 5.1.52, for redhat-linux-gnu (x86_64) -- -- Host: localhost Database: book -- ------------------------------------------------------ -- Server version 5.1.52
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- -- Table structure for table `book_book` --
DROP TABLE IF EXISTS `book_book`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `book_book` ( `id` int(11) NOT NULL DEFAULT '0', `name` varchar(50) DEFAULT NULL, `pub_date` varchar(200) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */;
-- -- Dumping data for table `book_book` --
LOCK TABLES `book_book` WRITE; /*!40000 ALTER TABLE `book_book` DISABLE KEYS */; INSERT INTO `book_book` VALUES (1,'The Django Book','2011-10-07'),(2,'The PF Book','2011-10-01'); /*!40000 ALTER TABLE `book_book` ENABLE KEYS */; UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;