1. Django概述

时间:2021-06-11 15:35:50

1.1 设计模型

Django,但它附带了一个你可以用python代码描述数据库布局的对象关系映射器

数据模型语法提供了许多丰富的方法来展现你的模型——到目前为止,它解决了多年来数据库模式问题。

简单的例子

#mysite/news/models.py

from django.db import models

class Reporter(models.Model):
full_name = models.CharField(max_length=70) def __str__(self):
return self.full_name class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter) def __str__(self):
return self.headline

1.2创建数据库

python manage.py migrate
migrate命令会查找所有可用的模型然后在你的数据库中,创建还不存在的数据库表

1.3使用API

使用一个便捷且功能丰富的Python API来访问你的数据。这些API是即时创建的,不需要代码生成:
# 导入模型
>>> from news.models import Reporter, Article # 查看实例.
>>> Reporter.objects.all()
[] # 创建一个实例.
>>> r = Reporter(full_name='John Smith') # 保存对象.
>>> r.save() # 查看Id.
>>> r.id
1 # 显示数据库的信息.
>>> Reporter.objects.all()
[<Reporter: John Smith>] # Fields are represented as attributes on the Python object.
>>> r.full_name
'John Smith' # Django provides a rich database lookup API.
>>> Reporter.objects.get(id=1)
<Reporter: John Smith>
>>> Reporter.objects.get(full_name__startswith='John')
<Reporter: John Smith>
>>> Reporter.objects.get(full_name__contains='mith')
<Reporter: John Smith>
>>> Reporter.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Reporter matching query does not exist. # Create an article.
>>> from datetime import date
>>> a = Article(pub_date=date.today(), headline='Django is cool',
... content='Yeah.', reporter=r)
>>> a.save() # Now the article is in the database.
>>> Article.objects.all()
[<Article: Django is cool>] # Article objects get API access to related Reporter objects.
>>> r = a.reporter
>>> r.full_name
'John Smith' # And vice versa: Reporter objects get API access to Article objects.
>>> r.article_set.all()
[<Article: Django is cool>] # The API follows relationships as far as you need, performing efficient
# JOINs for you behind the scenes.
# This finds all articles by a reporter whose name starts with "John".
>>> Article.objects.filter(reporter__full_name__startswith='John')
[<Article: Django is cool>] # Change an object by altering its attributes and calling save().
>>> r.full_name = 'Billy Goat'
>>> r.save() # Delete an object with delete().
>>> r.delete()

1.3在admin site中注册模型

一旦你的模型定义完毕之后,Django能自动创建一个专业的、可以用于生产环境的 管理界面 – 可以让认证的用户添加、修改和删除对象的一个站点。只需简单地在admin site中注册你的模型即可:

#mysite/news/admin.py

from django.contrib import admin

from . import models

admin.site.register(models.Article

1.4设计URLs

设计urls并包含映射

#mysite/news/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^articles/([0-9]{4})/$', views.year_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]

1.5编写视图

返回一个包含请求的页面内容的 HttpResponse对象, 或抛出一个异常如Http404,

通常,视图会根据参数检索数据,加载模版,使用数据渲染模版

#mysite/news/views.py

from django.shortcuts import render

from .models import Article

def year_archive(request, year):
a_list = Article.objects.filter(pub_date__year=year)
context = {'year': year, 'article_list': a_list}
return render(request, 'news/year_archive.html', context)

1.6设计模版

Django可以通过DIRS指定一个查找模版的列表

使用base.html ,进行渲染

Total:  概述的内容大概就是这样子了,之前简单的了解一点Django,感觉还是很方便的,看了廖学费的webapp教程,讲解太少,怪自己太愚也没怎么看懂。Django文档写的很详细,至少第一节是这样子的,相比之下更简单方便的多,特别是对于数据库的创建和引用,就这样吧。It is time to study django. Come on !