django入門教程1--設(shè)計模型、URL和模板

完整教程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 控制。

享用便捷的 API?

接下來,你就可以使用一套便捷而豐富的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()


完整教程https://www.daimapi.com/django/intro/overview/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 顯然,關(guān)系型數(shù)據(jù)庫的強大之處在于,表與表之間存在關(guān)聯(lián),Django提供了定義常見的三種類型的數(shù)據(jù)庫關(guān)系的方法:ma...
    第八共同體閱讀 1,372評論 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • 我是真的喜歡過沈先生,但我也知道我與你之間有如隔著一座大山,我翻過山,卻發(fā)現(xiàn)中間有一道深淵,深淵當中又有一條大河,...
    愚人tt閱讀 236評論 0 0
  • 再讀葉塞尼亞 夜在降臨 記憶在調(diào)零 時間從今到昔 死亡在每一刻 如柴的黃花 階石的冷的腳步 洞穿了世界的溫度 如星...
    月鈴閱讀 285評論 0 4

友情鏈接更多精彩內(nèi)容