前面的操作大多是在命令行內進行的,為了提高開發效率,下面我們將主要使用PyCharm開發博客。
使用PyCharm打開我們之前創建的工程,PyCharm會在mysite根目錄下創建.idea文件夾,不用管。打開工程后,先在file -> settings -> project:mysite -> project interpreter確認我們現在是否處于虛擬環境myvenv中。這里也列出了對應環境包含的第三方包當前版本和可升級版本,里面至少應該有Django和pip。
閑話少說,這一節我們來學習django網頁的三部曲:urls映射,views視圖和templates模板。簡單的說,urls包含了可接收的web地址,它根據web地址映射到views內的方法,views的方法獲取服務器數據并進行處理,返回templates,templates一般包含html和css,規定了呈現給用戶的網頁表現形式。
urls
打開mysite\mysite\urls.py
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
django默認添加了admin的url,因此當碰到admin開頭的url時就會去admin.site.urls里面尋找對應匹配。admin.site.urls里面是admin匹配的集合,這樣把有關admin的url寫到一個文件里面,有利于url管理。
django使用正則表達式匹配url。r'^admin/'
的r是Python中的非轉義原始字符串的意思,也就是說后面引號內的字符不進行轉義,寫了什么就是什么。匹配字符串開頭,表示只能匹配'admin/'開頭的url,對于'xadmin','123admin'等都不能匹配。如果去掉,也就是寫成r'admin/'
的格式,會發現'xadmin','123admin'都可以匹配到admin去。
我們添加一個url:
from django.conf.urls import url
from django.contrib import admin
from blog import views
urlpatterns = [
url(r'admin/', admin.site.urls),
url(r'^$', views.post_list),
]
$匹配字符串結尾,功能參照。因此'$'正好匹配了http://127.0.0.1:8000/
url已經寫好,現在就可以去實現views了。
views
我們在mysite\mysite\urls.py中把'^$'映射到了views.post_list,但是post_list還不存在,現在就來寫。
打開mysite\blog\views.py,寫入:
from django.shortcuts import render
from .models import Article
# Create your views here.
def post_list(request):
posts = Article.objects.filter(published_date__isnull=False).order_by('-published_date')
return render(request, 'blog/post_list.html', {'posts': posts})
方法post_list的參數是request,注意所有url指向的方法第一個參數都是request。這個方法很簡單,先獲取所有Article對象,過濾掉沒有發布時間的,剩下的再按發布時間倒序排序組成一個QuerySet,用post指向這個QuerySet。QuerySet我們在上一節剛剛提過。最后用render函數返回一個html模板blog/post_list.html,最后一個參數{'posts': posts}是用來給模板傳遞參數的。
這里要注意的是模板的定向。打開mysite\mysite\settings.py,
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
'DIRS'定義了模板根目錄在BASE_DIR\templates,BASE_DIR是什么呢?settings.py最上面已經寫了:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
哦,BASE_DIR就是項目根目錄,所以模板根目錄其實就是mysite\templates。我們在方法post_list里寫的"blog/post_list.html"其實就是mysite\templates\blog\post_list.html啊!現在知道模板定向到哪了,也知道要在哪寫模板了吧!
模板根目錄當然是可以改的,但是為了好管理,還是放在項目根目錄內比較好。
templates
在mysite\templates\blog內新建post_list.html,寫入:
<html>
<head>
<title>Simple Django Blog</title>
</head>
<body>
<div>
<h1><a href="/">Simple Django Blog</a></h1>
</div>
{% for post in posts %}
<div>
<p>published: {{ post.published_date }}</p>
<h1><a href="">{{ post.title }}</a></h1>
<p>{{ post.text|linebreaks }}</p>
</div>
{% endfor %}
</body>
</html>
<html>...</html>
之間的是html代碼,<head>...</head>
標記之間的內容用于描述頁面的頭部信息,如頁面的標題、作者、摘要、關鍵詞、版權、自動刷新等信息。這里<title>...</title>
就包住了標題Simple Django Blog。<body>...</body>
標記之間的內容即為頁面的主體內容,包括一個一級標題<h1>...</h1>
和一個顯示所有文章的標題和對應內容的for循環{% for post in posts %}...{% endfor %}
。形如{{ post.published_date }}表示變量,post_list傳過來的參數{'posts': posts}在for循環內用python的模板標簽獲取文章的各種內容。
現在再打開http://127.0.0.1:8000,就可以看到我們的內容了。
P.S. 上圖選項卡標題旁有個圖標,正常來說由于還沒有設置圖標這里應該是一個空白文件的標識,這是因為我之前做網頁時候的緩存沒有清干凈。
P.P.S 關于PyCharm的使用網上教程很多,這里不多說什么了。Shift+F10編譯運行,改動后不需要重新編譯,Ctrl+S保存當前頁面改動,PyCharm會自動重新編譯運行。另外,若在命令框內用runserver啟動,改動工程后也不需要重新啟動,也會自動重新編譯運行的。
Django的三部曲:urls、views和templates已經初步完成,說明我們的博客已經初具規模了!
2016.10.21