七日Python之路--第九天(blog与Django)
在做models之前,最好将https://docs.djangoproject.com/en/1.6/ 中的the model layer 细读。
(一)琐碎
(1)*args和**kwargs。的使用
在Django中经常见到含有这两个参数的函数。*args可代表任意个参数:func(arg, *args)代表这个函数可以有一个或更多个参数。可以理解成一个参数list。**kwargs可以代表任意个K=V形式的参数。大体就是这样。
(2)不经意间,被MySQL的编码折磨。Ubuntu下修改一直有问题。
头痛。折腾了半天还是没搞好,修改/etc/mysql/my.cnf 文件后,重启Mysql服务失败,改回去就可以。但是字符集问题还是没搞好。暂时先用着SQLite3吧。:-(
(二)Blog
(1)暂且叫做Dlog吧。创建项目及应用。第一步毫无疑问,配置settings.py .使用MySQL数据库。
""" Django settings for mysite project. For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = ‘4@01u6jlly!dr*x&91$p@bqo4w(%0t0g8zp+6g9s650(&glpkv‘ # SECURITY WARNING: don‘t run with debug turned on in production! DEBUG = True TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = ( ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, #add by dizzy in 2014-07-29 19:36:23 ‘dlog‘, ) MIDDLEWARE_CLASSES = ( ‘django.contrib.sessions.middleware.SessionMiddleware‘, ‘django.middleware.common.CommonMiddleware‘, ‘django.middleware.csrf.CsrfViewMiddleware‘, ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, ‘django.contrib.messages.middleware.MessageMiddleware‘, ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, ) ROOT_URLCONF = ‘mysite.urls‘ WSGI_APPLICATION = ‘mysite.wsgi.application‘ # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘dlog‘, ‘USER‘: ‘root‘, ‘PASSWORD‘:‘944898186‘, ‘HOST‘: ‘localhost‘, ‘PORT‘: ‘3306‘, } } # Internationalization # https://docs.djangoproject.com/en/1.6/topics/i18n/ LANGUAGE_CODE = ‘zh-cn‘ TIME_ZONE = ‘Asia/Shanghai‘ USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATIC_URL = ‘/static/‘
(2)创建dlog应用,并设置models.py。如下,当然这只是初步设置
#coding=utf-8 from django.db import models # Create your models here. class BaseModel(models.Model): name = models.CharField(u‘名称‘, max_length=20) crttime = models.DateTimeField(u‘创建时间‘,auto_now_add=True) def __unicode__(self): return self.name class Meta: abstract=True class Author(BaseModel): class Meta: db_table = ‘dlog_author‘ verbose_name = u‘作者‘ class Theme(BsaeModel): class Meta: db_table = ‘dlog_theme‘ verbose_name = u‘主题‘ class Tag(BaseModel): class Meta: db_table = ‘dlog_tage‘ verbose_name = u‘标签‘ class Category(BaseModel): class Meta: db_table = ‘dlog_category‘ verbose_name = u‘分类‘ class Article(BaseModel): title = models.CharField(u‘标题‘, max_length=100) author = models.ForeignKey(Author, verbose_name=u‘作者‘) theme = models.ForeignKey(Theme, verbose_name=u‘主题‘) tags = models.ManyToManyField(Tag, blank=True, verbose_name=u‘标签‘) category = models.ForeignKey(Category, verbose_name=u‘分类‘) abstract = models.TextField(u‘摘要‘) created_time = models.DateTimeField(u‘创建时间‘, auto_now_add=True) updated_time = models.DateTimeField(u‘修改时间‘, auto_now=True) content = models.TextField(u‘正文‘) click_count = models.IntegerField(u‘点击量‘, default=0, editable=False) comment_count = models.IntegerField(u‘评论数‘,default=0, editable=False) def __unicode__(self): return self.title def click(self): pass class Meta: db_table = ‘dlog_article‘ verbose_name = u‘文章‘
对应的SQL:
dizzy@dizzy-pc:~/dizzy/mysite$ ./manage.py sql dlog BEGIN; CREATE TABLE `dlog_author` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(20) NOT NULL, `crttime` datetime NOT NULL ) ; CREATE TABLE `dlog_theme` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(20) NOT NULL, `crttime` datetime NOT NULL ) ; CREATE TABLE `dlog_tage` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(20) NOT NULL, `crttime` datetime NOT NULL ) ; CREATE TABLE `dlog_category` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(20) NOT NULL, `crttime` datetime NOT NULL ) ; CREATE TABLE `dlog_article_tags` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `article_id` integer NOT NULL, `tag_id` integer NOT NULL, UNIQUE (`article_id`, `tag_id`) ) ; ALTER TABLE `dlog_article_tags` ADD CONSTRAINT `tag_id_refs_id_e377ffd2` FOREIGN KEY (`tag_id`) REFERENCES `dlog_tage` (`id`); CREATE TABLE `dlog_article` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(20) NOT NULL, `crttime` datetime NOT NULL, `title` varchar(100) NOT NULL, `author_id` integer NOT NULL, `theme_id` integer NOT NULL, `category_id` integer NOT NULL, `abstract` longtext NOT NULL, `createdtime` datetime NOT NULL, `updatedtime` datetime NOT NULL, `content` longtext NOT NULL, `click_count` integer NOT NULL, `comment_count` integer NOT NULL ) ; ALTER TABLE `dlog_article` ADD CONSTRAINT `author_id_refs_id_c7985c8d` FOREIGN KEY (`author_id`) REFERENCES `dlog_author` (`id`); ALTER TABLE `dlog_article` ADD CONSTRAINT `category_id_refs_id_b7340953` FOREIGN KEY (`category_id`) REFERENCES `dlog_category` (`id`); ALTER TABLE `dlog_article` ADD CONSTRAINT `theme_id_refs_id_d135d084` FOREIGN KEY (`theme_id`) REFERENCES `dlog_theme` (`id`); ALTER TABLE `dlog_article_tags` ADD CONSTRAINT `article_id_refs_id_61edf8a3` FOREIGN KEY (`article_id`) REFERENCES `dlog_article` (`id`); COMMIT;
(3)设置下admin后台显示部分。
-- 2014年07月30日03:17:31
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。