上一篇我們學習如何配置Django開發(fā)環(huán)境,這一篇文章我們繼續(xù)來學習Django的內(nèi)容猿挚!
我們已經(jīng)創(chuàng)建了一個項目咐旧,大家還記得創(chuàng)建項目的命令嗎?django-admin startproject + 項目名
绩蜻,我們接著來創(chuàng)建我們應(yīng)用铣墨,命令為:python manage.py startapp + 應(yīng)用名
。查看一下應(yīng)用的目錄:
~/Desktop/projects/test1$ tree booktest/
booktest/
├── admin.py
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
1 directory, 6 files
接起來我們需要在booktest
下的models.py
文件中編寫我應(yīng)用模板:
from django.db import models
# Create your models here.
class bookInfo(models.Model):
btitle = models.CharField(max_length=20)
bpub_data = models.DateField()
class hreoInfo(models.Model):
hname = models.CharField(max_length=10)
hgender = models.BooleanField()
hcontent = models.CharField(max_length=1000)
hbook = models.ForeignKey(bookInfo)
創(chuàng)建完模板我們在命令行運行一下办绝,看看效果:
~/Desktop/projects/test1$ python manage.py runserver 8080
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
August 13, 2018 - 08:55:09
Django version 1.8.2, using settings 'test1.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.
[13/Aug/2018 08:55:58]"GET / HTTP/1.1" 200 1767
[13/Aug/2018 08:55:58]"GET /favicon.ico HTTP/1.1" 404 1935
我們點擊結(jié)果中提示的本地鏈接:
網(wǎng)頁顯示效果
生成數(shù)據(jù)表:
第一步需要在settings.py
文件中注冊應(yīng)用伊约,
注冊應(yīng)用
第二步就是用命令行生成遷移文件:
~/Desktop/projects/test1$ python manage.py makemigrations
Migrations for 'booktest':
0001_initial.py:
- Create model bookInfo
- Create model hreoInfo
第三步就是執(zhí)行遷移文件:
~/Desktop/projects/test1$ python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: booktest, sessions, auth, admin, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying booktest.0001_initial... OK
Applying sessions.0001_initial... OK
總結(jié):本篇文章主要是講了,創(chuàng)建項目應(yīng)用孕蝉,編寫我們應(yīng)用模板屡律,如何生成數(shù)據(jù)表(注冊應(yīng)用,生成遷移文件降淮,執(zhí)行遷移文件)的學習操作超埋,希望對大家有幫助,Django
學習是一個持續(xù)過程骤肛,我會記錄我的學習內(nèi)容教程纳本,歡迎大家一起學習。