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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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