django1.9 学习路径 http://python.usyiyi.cn/django/intro/tutorial02.html
django 中url路径带参数,在view中应该如何处理
应该将拦截参数的正则表达式扩起来,用来明确告诉django,这是view 函数所以需要的参数,如下
(r'^time/plus/(d{1,2})/$', hours_ahead),
否则,页面会把参数当成普通路径,报出TypeError错误,如下:
无论怎么弄,django都会报出 TemplateDoesNotExist 错误,
(同时已配置TEMPLATE_DIR),如图:
原因:没有配置TEMPLATE的DIR参数:
补上:
model
1.创建一个项目
django-admin startproject projectName
2.运行开发服务器
python manage.py runserver
3.为项目添加应用
python manage.py startapp appName
4.在项目的应用中创建相应模型
appName/models.py
from django.db import models
class Model1(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Model2(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
5.在project/setting.py中激活模型(插入app)
projectName/setting.py
INSTALLED_APPS = (
appName,
)
6.创建迁移文件,让django保存app的变化
python manage.py makemigrations appName
7.查看模型对应的sql语句
python manage.py sqlmigrate appName 0001(迁移文件名称)
8.创建模型对应的数据库表
python manage.py migrate
#################################################################
#three-step guide to making model changes #
#1.修改你的模型(在models.py文件中) #
#2.运行python manage.py makemigrations ,为这些修改创建迁移文件 #
#3.运行python manage.py migrate ,将这些改变更新到数据库中 #
#################################################################
admin账号
admin 管理并设置不同的样式:
1 from django.contrib import admin 2 from models import Publisher, Author, Book 3 # Register your models here. 4 5 class PublisherAdmin(admin.ModelAdmin): 6 fields = ['name', 'address', 'city', 'state_province', 'country', 'website'] 7 8 class AuthorAdmin(admin.ModelAdmin): 9 fieldsets = [ 10 ('Name', {'fields':['first_name', 'last_name']}), 11 ('Email', {'fields':['email']}), 12 ] 13 14 class BookAdmin(admin.ModelAdmin): 15 fieldsets = [ 16 ('Title', {'fields':['title']}), 17 ('Authors', {'fields':['authors'], 'classes':['collapse']}), 18 ('Publisher', {'fields':['publisher']}), 19 (None, {'fields':['publication_date']}), 20 ] 21 admin.site.register(Publisher, PublisherAdmin) 22 admin.site.register(Author, AuthorAdmin) 23 admin.site.register(Book, BookAdmin)