您好,登錄后才能下訂單哦!
除了以上三層之外,還需要一個URL分發(fā)器,它的作用是將一個個URL的頁面請求分發(fā)給不同的View處理,View再調(diào)用相應(yīng)的Model和Template
1-1. 創(chuàng)建虛擬環(huán)境
[root@room9pc01 day20]# mkdir -pv /opt/py3env
[root@room9pc01 day20]# ls /opt/py3env
[root@room9pc01 day20]# python3 -m venv /opt/py3env/djenv
1-2. 激活虛擬環(huán)境
[root@room9pc01 day20]# source /opt/py3env/djenv/bin/activate
激活之后,運行python默認(rèn)是py3
1-3. 默認(rèn)激活之后,安裝django,就是安裝在了虛擬環(huán)境下
(djenv) [root@room9pc01 day20]# pip install django==1.11.6
(djenv) [root@room9pc01 day20]# pip install pymysql
注意:如果以上方式無法安裝可以采用如下方式
1) 更換鏡像站點
(djenv) [root@room9pc01 day20]# cat ~/.pip/pip.conf
[global]
index-url=http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com
2)查看當(dāng)前環(huán)境安裝的軟件包
(djenv) [root@room9pc01 day20]# pip freeze
asn1crypto==0.24.0
cffi==1.11.5
cryptography==2.3.1
Django==1.11.6
idna==2.7
pycparser==2.18
PyMySQL==0.9.2
pytz==2018.5
six==1.11.0
(djenv) [root@room9pc01 day20]# pip freeze > pkgs.txt
(djenv) [root@room9pc01 day20]# pip install --download /存儲目錄 pkgs.txt
(djenv) [root@room9pc01 day20]#pip install 文件名
2-1. 創(chuàng)建項目
(djenv) [root@room9pc01 day20]# django-admin startproject mysite
(djenv) [root@room9pc01 day20]# cd mysite/
2-2. pycharm設(shè)置,使用虛擬環(huán)境
file -> settings -> project xxx -> project interpreter -> 添加現(xiàn)有環(huán)境找到/opt/py3env/djenv/bin/python
2-3. 為django項目創(chuàng)建數(shù)據(jù)庫
(djenv) [root@room9pc01 mysite]# mysql -uroot -ptedu.cn
MariaDB [(none)]> create database dj_tedu default charset utf8;
2-4. 修改django的設(shè)置 settings.py
ALLOWED_HOSTS = '*' # 允許所有客戶端訪問
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dj_tedu',
'USER': 'root',
'PASSWORD': 'tedu.cn',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
2-5. 加載連接mysql的模塊,在__init__.py中寫入以下內(nèi)容
import pymysql
pymysql.install_as_MySQLdb()
2-6. 啟動服務(wù)
(djenv) [root@room8pc16 mysite]# python manage.py runserver 0:8000
2-7. 生成數(shù)據(jù)庫
(djenv) [root@room9pc01 mysite]# python manage.py migrate
2-8. 創(chuàng)建后臺管理員
(djenv) [root@room9pc01 mysite]# python manage.py createsuperuser
2-9. 啟動服務(wù)后,訪問http://127.0.0.1:8000/admin
3-1. 新建視圖
# vim mysite/views.py
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse('<h2>Hello World!</h2>')
3-2. 修改urls.py,訪問主頁時,使用views模塊的index函數(shù)響應(yīng)
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.index, name='index'), # name='index'是首頁URL的
]
4-1. 創(chuàng)建應(yīng)用
(djenv) [root@room9pc01 mysite]# python manage.py startapp polls
4-2. 在項目中注冊應(yīng)用,修改settings.py
INSTALLED_APPS = [
... ...
'polls',
]
4-3. 規(guī)劃url,polls應(yīng)用下有很多個頁面,但是每個頁面訪問時,都以http://127.0.0.1/polls開頭,如http://127.0.0.1/polls/1 polls應(yīng)用有自己的URLConf,當(dāng)訪問到http://127.0.0.1/polls開頭的網(wǎng)址都交給polls應(yīng)用的urls.py
# vim mysite/urls.py
from django.conf.urls import url, include
urlpatterns = [
... ...
url(r'^polls/', include('polls.urls')),
]
# vim polls/urls.py
from django.conf.urls import url
urlpatterns = [
url(r'^$'),
4-4. 創(chuàng)建應(yīng)用的url映射文件
# vim polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.polls_index, name='polls_index'),
]
4-5. 創(chuàng)建視圖
# vim polls/views.py
from django.shortcuts import render, HttpResponse
def polls_index(request):
return HttpResponse('<h2>Polls index</h2>')
4-6. 創(chuàng)建模版
(1) 創(chuàng)建模板目錄
[root@room9pc01 mysite]# mkdir -p polls/templates/polls #templates是django固定的,polls是存放網(wǎng)頁文件
(2)創(chuàng)建模板文件
# vim polls/templates/polls_index.html
(3)更改視圖views.py
def polls_index(request):
return render(request,'polls/polls_index.html')
4-7. 添加boostrap樣式
將前面解壓的bootstrap拷貝過來
[root@room9pc01 mysite]# cp -r ../../day18/static/ polls/
在模版文件polls_index.html中添加引用
文件頂部增加一行:
{% load staticfiles %}
head標(biāo)簽中加入
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
5-1. 創(chuàng)建ORM映射類
# vim polls/models.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
publish_date = models.DateTimeField()
def __str__(self):
return self.question_text
class Choice(models.Model):
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
question = models.ForeignKey(Question, models.CASCADE)
def __str__(self):
return self.choice_text
5-2. 生成數(shù)據(jù)庫中的表
(djenv) [root@room8pc16 mysite]# python manage.py makemigrations
(djenv) [root@room8pc16 mysite]# python manage.py migrate
5-3. 在數(shù)據(jù)庫中show tables查看表名
5-4. 查看表中字段
(1)django會在表中自動加入一列id,作為主鍵
(2)表中的每個字段與類變量相同
(3)外鍵是類變量名加上_id
5-5. 把模型注冊到后臺管理界面,修改polls/admin.py如下:
from django.contrib import admin
from .models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
5-6. 訪問http://127.0.0.1:8000/admin,在后臺可以添加數(shù)據(jù)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。