您好,登錄后才能下訂單哦!
上一章https://blog.51cto.com/yht1990/2382801
本章節(jié)官網(wǎng)詳細(xì)參考:https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial02/
1、配置pymysql代替MySQLdb
在需要的項(xiàng)目中(D:\mysite\polls下的init.py)添加如下內(nèi)容
import pymysql
pymysql.install_as_MySQLdb()
2、服務(wù)器創(chuàng)建mysite數(shù)據(jù)庫(kù),配置權(quán)限允許訪問(wèn)數(shù)據(jù)庫(kù)的IP
3、配置允許所有主機(jī)訪問(wèn)
mysite\settings.pyALLOWED_HOSTS = ["*"]
4、數(shù)據(jù)庫(kù)配置
mysite\settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '192.168.20.217',
'PORT': '3306',
}
}
5、配置包含應(yīng)用
mysite\settings.py
INSTALLED_APPS = [
'polls.apps.PollsConfig', #添加的配置
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
6、注釋csrf驗(yàn)證
mysite\settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
7、創(chuàng)建存放靜態(tài)html文件目錄,所有靜態(tài)文件放此目錄下
即建一個(gè)templates再建polls
8、創(chuàng)建存放bootstarp文件目錄,所有boostarp文件放此目錄下
即建一個(gè)static再建polls
9、創(chuàng)建模型
polls/models.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
10、建表
1)為模型的改變生成遷移文件。
D:\mysite>python manage.py makemigrations
Migrations for 'polls':
polls\migrations\0001_initial.py
- Create model Choice
- Create model Question
- Add field question to choice
2)開(kāi)始建表python manage.py migrate
一、django管理頁(yè)面
1)創(chuàng)建一個(gè)管理員賬號(hào)
2) 向管理頁(yè)面加入投票應(yīng)用
polls/admin.py
from django.contrib import admin
from .models import Question
admin.site.register(Question)
3)重啟訪問(wèn)
ide重啟項(xiàng)目,訪問(wèn):http://127.0.0.1:8000/admin/
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。