2018-12-15 Django Site

make a site

Environment

  • laptop thinkpad
  • windows 10
  • pycharm
  • python 3.6.5
  • chrome

use python virtual environment

you'd better use virtual environment for python.

python3.6 -m venv my_env
source my_env/bin/activate      

then you go to the (my_env) mode

install django

pip install django
python
>>> import django
>>> django.get_version()

Start a site

django-admin startproject mysite
cd mysite
python manage.py migrate

migrate is to make the relevant database to store useful site information. such as administrator user.
By default use sqlite3 for database system.

start server

python manage.py runserver

*This is a only test mode, if you want to use django on site, need to deploy it in your server. *

python manage.py start blog

edit blog/models.py

from django.db import models
# Create your models here.
from django.utils import timezone
from django.contrib.auth.models import User

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft','Draft'),
        ('published','Published'),
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,unique_for_date='publish')
    author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

class Meta:
    ordering = ('-publish',) 

def __str__(self):
    return self.titl

in the root directory of the projeccts, run the following command:

python manage.py makemigrations blog

then django will get a file

blog/migrations/0001_initals.py

then you can print the following command to see what SQL code django will execute in the database to create table for our model.

python manage.py sqlmigrate blog 0001

the out put will be like this, depending what database system you are use:

BEGIN;
--
-- Create model Post
--
CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(250) NOT NULL, "slug" varchar(250) NOT NULL, "body" text NOT NULL, "publish" datetime NOT NULL, "created" datetime NOT NULL, "updated" datetime NOT NULL, "status" varchar(10) NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "blog_post_slug_b95473f2" ON "blog_post" ("slug");
CREATE INDEX "blog_post_author_id_dd7a8485" ON "blog_post" ("author_id");
COMMIT;

the use run the following command to apply the existing migrations:

python manage.py migrate

Anytime you want to revise models.py you need to run makemigrations and migrate to to keep the database sync with it.

  1. each model is import from django.db.models.Models
  2. attribute => field
  3. object => record

python manage.py sqlmigrate blog 0001
python manage.py createsuperuser

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子尿赚,更是在濱河造成了極大的恐慌,老刑警劉巖贴妻,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苍碟,死亡現(xiàn)場離奇詭異,居然都是意外死亡翰守,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門话肖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來北秽,“玉大人,你說我怎么就攤上這事最筒『孛ィ” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵床蜘,是天一觀的道長掠归。 經(jīng)常有香客問我,道長悄泥,這世上最難降的妖魔是什么虏冻? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮弹囚,結(jié)果婚禮上厨相,老公的妹妹穿的比我還像新娘。我一直安慰自己鸥鹉,他們只是感情好蛮穿,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著毁渗,像睡著了一般践磅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上灸异,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天府适,我揣著相機與錄音,去河邊找鬼肺樟。 笑死檐春,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的么伯。 我是一名探鬼主播疟暖,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼田柔!你這毒婦竟也來了俐巴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤硬爆,失蹤者是張志新(化名)和其女友劉穎欣舵,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體摆屯,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡邻遏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了虐骑。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片准验。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖廷没,靈堂內(nèi)的尸體忽然破棺而出糊饱,到底是詐尸還是另有隱情,我是刑警寧澤颠黎,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布另锋,位于F島的核電站,受9級特大地震影響狭归,放射性物質(zhì)發(fā)生泄漏夭坪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一过椎、第九天 我趴在偏房一處隱蔽的房頂上張望室梅。 院中可真熱鬧,春花似錦疚宇、人聲如沸亡鼠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽间涵。三九已至,卻和暖如春榜揖,著一層夾襖步出監(jiān)牢的瞬間勾哩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工举哟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留钳幅,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓炎滞,卻偏偏與公主長得像敢艰,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子册赛,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容