完整教程https://www.daimapi.com/django/intro/overview/
初識 Django?
Django 最初被設(shè)計用于具有快速開發(fā)需求的新聞類站點,目的是要實現(xiàn)簡單快捷的網(wǎng)站開發(fā)。以下內(nèi)容簡要介紹了如何使用 Django 實現(xiàn)一個數(shù)據(jù)庫驅(qū)動的 Web 應(yīng)用。
為了讓您充分理解 Django 的工作原理,這份文檔為您詳細描述了相關(guān)的技術(shù)細節(jié),不過這并不是一份入門教程或者是參考文檔(我們當然也為您準備了這些)。如果您想要馬上開始一個項目,可以從?實例教程?開始入手,或者直接開始閱讀詳細的?參考文檔?。
設(shè)計模型?
Django 無需數(shù)據(jù)庫就可以使用,它提供了?對象關(guān)系映射器?通過此技術(shù),你可以使用 Python 代碼來描述數(shù)據(jù)庫結(jié)構(gòu)。
你可以使用強大的?數(shù)據(jù)-模型語句?來描述你的數(shù)據(jù)模型,這解決了數(shù)年以來在數(shù)據(jù)庫模式中的難題。以下是一個簡明的例子:
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, on_delete=models.CASCADE)
? ? def __str__(self):
? ? ? ? return self.headline
應(yīng)用數(shù)據(jù)模型?
然后,運行 Django 命令行工具來創(chuàng)建數(shù)據(jù)庫表:
$ python manage.py migrate
這個migrate命令查找你所有可用的模型并將在數(shù)據(jù)庫中創(chuàng)建那些不存在的表,還可提供了可選的豐富 schema 控制。
接下來,你就可以使用一套便捷而豐富的Python API訪問你的數(shù)據(jù)。這些 API 是即時創(chuàng)建的,而不用顯式的生成代碼:
# Import the models we created from our "news" app
>>> from news.models import Article, Reporter
# No reporters are in the system yet.
>>> Reporter.objects.all()
<QuerySet []>
# Create a new Reporter.
>>> r = Reporter(full_name='John Smith')
# Save the object into the database. You have to call save() explicitly.
>>> r.save()
# Now it has an ID.
>>> r.id
1
# Now the new reporter is in the database.
>>> Reporter.objects.all()
<QuerySet [<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()
<QuerySet [<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()
<QuerySet [<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')
<QuerySet [<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()