第一個 Django Project開發
來源:程序員人生 發布時間:2015-02-03 08:46:56 閱讀次數:2166次
本篇文章是 官網https://docs.djangoproject.com/en/1.7/intro/tutorial01/” 的實踐版本。由于原文有較多的解釋成份和用英語書寫不便于快速進入Django的開發。所以才有本文。
http://dmyz.org/archives/110
Part 1. 環境搭建測試
如需轉載請注明出處:*************************** 謝謝。
1. 環境
Ubuntu 13.10 # cat /etc/issue 進行查看
Python 3.3.2+ # python -V 進行查看
Django 1.7.3. # python -c "import django; print(django.get_version())" 進行查看
http://blog.csdn.net/michael_kong_nju/article/details/42878651 在這篇文章中記錄了,如何安裝和升級
如果python的版本有區分的話,比如2.x.x那末在語法上會有區分。
2. 創建1個工程
我們將工程放在 /opt/django_programming/下面。
# cd /opt/django_programming
# django-admin.py startproject mysite %%跟官方教程1致,我們新建的project取名為 mysite.
然后我們就完成了1個mysite項目的創建。我們用 tree命令查看剛才創建的結構。
root@michaelpc:/opt/django_programming# tree
.
└── mysite %%%%外部的這個mysite就是剛才我們project的名字。這里就是1個容器,用來寄存project的1些內容。
├── manage.py
└── mysite
├── __init__.py
├── settings.py %%%% Django Project的配置文件。
├── urls.py
└── wsgi.py
注意這里面作為Django的快速入門,我們使用的是python自帶的Sqlite3數據庫。如果是實際的開發商業項目不主張這么開做。可以查看后面的博文:*************
# vim mysite/settings.py %%%%%%打開配置文件,找到DATABASE數組內容,對DATABASE不做修改。
DATABASES = {
default': {
'ENGINE': 'django.db.backends.sqlite3', %%%%默許使用的sqlite3
數據庫。如果以后需要關聯別的如mysql 只需要在這里進行處理。
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), %%名稱
}
}
將TIME_ZONE修改成: TIME_ZONE = 'Asia/Shanghai'. 保存退出以后履行:
# python manage.py migrate
至此數據庫建立完成。
這里我們使用Django自帶的服務器,只需要使用下面命令就能夠開啟:(由于此命令不能中斷,所以建議開啟新的終端來運行這個命令)
# python manage.py runserver %%%%%%%%%也可有用這個命令: python manage.py runserver 0.0.0.0:8000 指定ip和端口,這里使用默許
然后在服務器中打開http://127.0.0.1:8000/可以看到1個歡迎界面。
至此1個project已完成了,即我們完成了1個project的架構,下面我們開始建立具體的 利用(app)
5. 創建APP
這里創建 polls這個投票利用。 在與manage.py相同目錄下履行下面命令:
# python manage.py startapp polls
然后我們發現在當前目錄下新建了1個polls目錄,用tree命令查看文件結構.以后我們修改 polls下面的models.py文件。簡單來講models存儲了我們對數據模式格式的定義
# vim polls/models.py
然后在 # Create your models here 下面加入:
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
保存退出。這里定義了python的兩個類分別是 Question 和 Choice
6. 激活polls的model
修改 mysite/setting.py中的 INSTALL_APPS數組
# vim mysite/setting.py
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在INSTALL_APPS 數組的后面加入 'polls',使得下次創建APP時候會將polls1起創建。</span>
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
保存退出,履行:
# python manage.py makemigrations polls
出現以下履行結果:
Migrations for 'polls':
0001_initial.py:
- Create model Question
- Create model Choice
- Add field question to choice
運行下面語句查看返回的sql語句。
#python manage.py sqlmigrate polls 0001
下面是我的終端輸出的信息:
BEGIN;
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
CREATE TABLE "polls_choice__new" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id"));
INSERT INTO "polls_choice__new" ("choice_text", "question_id", "id", "votes") SELECT "choice_text", NULL, "id", "votes" FROM "polls_choice";
DROP TABLE "polls_choice";
ALTER TABLE "polls_choice__new" RENAME TO "polls_choice";
CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id");
運行下面語句使得生成數據表
# python manage.py migrate
輸出以下信息,數據表創建成功。
root@michaelpc:/opt/django_programming/mysite# python manage.py migrate
Operations to perform:
Apply all migrations: sessions, admin, contenttypes, polls, auth
Running migrations:
Applying polls.0001_initial... OK
至此完成了polls模型的創建。
7. 下面我們嘗試使用sqlite在django中的api.
# python manage.py shell %%進入交互界面
順次:
>>>from polls.models import Question, Choice %%導入我們剛才床家你的模型類
>>>Question.objects.all()
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now()) %%添加數據
>>> q.save()
如果沒有出錯表明ok,還有好多API可供使用,這里就不逐一論述,可以做參考官方網站的教程:
https://docs.djangoproject.com/en/1.7/intro/tutorial01/#writing-your-first-django-app-part⑴
至此我們完成了環境的搭建,并且創建了project和app,下面我們具體的做開發。
Part2. 實例開發
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈