1. 創(chuàng)建項目與app
創(chuàng)建項目
django-admin startproject firstsite
創(chuàng)建app
python manage.py startapp firstapp
2.settings里面添加app
編輯 firstsite/settings.py
:
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'firstapp', # 添加剛才創(chuàng)建的app
)
3. 編輯models層
打開 firstapp/models.py
导盅,編輯如下內(nèi)容:
from django.db import models
# 創(chuàng)建一個叫 Article 的類冈钦,它繼承自django.db.models.Model慕购,
# headline 和 content 是它的屬性辅柴,都繼承自django.db.models.Field
# 并定義了屬性的數(shù)據(jù)類型和限制
class Article(models.Model):
headline = models.CharField(null=True, blank=True, max_length=200)
content = models.TextField(null=True, blank=True)
# 讓實例在后臺管理界面顯示自己的headline
def __str__(self):
return self.headline
4. 創(chuàng)建和合并數(shù)據(jù)庫
對 Model 做了修改后讥巡,使用 makemigrations
命令,你的 Model 會被掃描, 然后與 migrations
文件夾中以前的版本作比較, 然后生成本次遷移文件衍慎。
python manage.py makemigrations
有了新的 migration
文件甥角,就可以使用 migrate
命令修改數(shù)據(jù)庫模式:
python manage.py migrate
每次修改完 Model 后都應該執(zhí)行上訴命令。
5.設置模板路徑
在 firstapp 文件夾內(nèi)創(chuàng)建 templates
和 static
兩個文件夾呼寸,html 文件放入 templates
文件夾艳汽,css、js 和圖片等文件放入 static
中.
文檔目錄樹如下:
然后在 settings.py
中修改模板路徑:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 模板路徑对雪,意思是:找到根目錄河狐,在根目錄下添加 templates
'DIRS': [os.path.join(BASE_DIR, 'firstapp', 'templates').replace('\\','/')],
'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',
],
},
},
]
6.創(chuàng)建管理后臺和超級管理員
在 firstsite 目錄下打開命令行,輸入:
python manage.py createsuperuser
按要求慌植,填上用戶名甚牲、郵箱、密碼蝶柿,打開: http://127.0.0.1:8000/admin/ 丈钙,登錄管理界面,就能看到以下界面:
要讓 Article 出現(xiàn)在管理界面戒洼,需要在 admin.py
引入相應的數(shù)據(jù)項:
from django.contrib import admin
from firstapp.models import Article # c從模板層引入Article
admin.site.register(Article) # 在后臺管理中添加 Article
這樣后臺就多了 Article 一項了靴寂。
你可以點進去添加新內(nèi)容:
7.在 View 中獲取 Model 的數(shù)據(jù)
編輯 views.py
剖踊,引入 model 中文章列表祥山,然后渲染網(wǎng)頁:
from django.shortcuts import render
from firstapp.models import Article # 從modles引入剛創(chuàng)建的類Article
from django.template import Template,Context
def index(request):
# 全部的文章列表
article_list = Article.objects.all()
# 一個字典結構,包裝要渲染的數(shù)據(jù)
context = {'article_list': article_list}
# 渲染,包含三個參數(shù):request麻裁、模板 html 的名字手销、context
index_page = render(request, 'first_web.html', context)
return index_page
8.在 Template 中增加動態(tài)內(nèi)容
編輯 templates
文件夾里相應的 html 文件(這里我命名為:first_web.html
):
<!DOCTYPE html>
<!-- Django 的靜態(tài)文件標簽 -->
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<title>first_web</title>
<!-- 引入 static 文件夾里面的css樣式 -->
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title">
</head>
<body>
<div class="ui segment">
<h1 class="ui centered header">Your Blog</h1>
</div>
<div class="ui basic segment">
<!-- 引入 static 文件夾里面的圖片 -->
![]({% static )
</div>
<!-- 用一個 for 循環(huán)來順序展示 article 里的內(nèi)容 -->
{% for article in article_list %}
<div class="ui very padded segment">
<h2>
<i class="star icon"></i>
<!-- article 的標題 -->
{{article.headline}}
</h2>
<!-- article 的內(nèi)容 -->
<p>{{article.content}}</p>
</div>
<br>
{% endfor %}
<!-- for 循環(huán)結束 -->
</body>
</html>
9.在 URL 中分配網(wǎng)址
編輯 urls.py
,作用是讓鏈接可以被訪問:
from django.conf.urls import include, url
from django.contrib import admin
# 引入視圖
from firstapp.views import index
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', index, name='index'),
]
現(xiàn)在打開:http://127.0.0.1:8000/index 柄错,可以看到網(wǎng)站已經(jīng)創(chuàng)建好了。