1:django-admin.py startproject 項(xiàng)目名
2:cd 項(xiàng)目名
3:修改setting.py
(1):ALLOWED_HOSTS = []=>ALLOWED_HOSTS = [“*”]
(2):LANGUAGE_CODE = 'en-us' => LANGUAGE_CODE = 'zh-hans'
(3):TIME_ZONE = 'UTC' => TIME_ZONE = 'Asia/Shanghai'
4:創(chuàng)建app
django-admin.py startproject app名
5:修改setting.py
INSTALLED_APPS數(shù)組增加=>'app名'
6:項(xiàng)目目錄下新增utils文件夾用來封裝多個(gè)app公共方法
7:app目錄下新增util文件用來封裝耽擱app通用方法
8:app目錄下新增view文件夾,里面存放各個(gè)頁面的邏輯代碼
9:app目錄下新增路由文件(urls.py),用來存放此app下的所有路由
格式:
from django.urls import path
from .views import wx_pay
urlpatterns = [
path("test", wx_pay.wx_pay().test, name="test"), # 測(cè)試
]
10:添加app路由至項(xiàng)目路由中;修改項(xiàng)目目錄下的urls.py
from django.contrib import admin
from django.urls import path, include
from H5 import urls as h5_urls
urlpatterns = [
path('admin/', admin.site.urls),
path("h5/", include(h5_urls))
]
至此django項(xiàng)目目錄結(jié)構(gòu)已經(jīng)配置結(jié)束了,后面即可進(jìn)入開發(fā)環(huán)節(jié)了。
開發(fā)時(shí)我們往往會(huì)用到數(shù)據(jù)庫;下面介紹下怎么配置數(shù)據(jù)庫
11:修改項(xiàng)目目錄下的setting.py
默認(rèn):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
修改后:
DATABASES = {
'default': {
‘ENGINE' :'django.db.backends.mysql',
'NAME':'數(shù)據(jù)庫名(以下稱DATABASE1)',
'USER':'用戶名',
‘PASSWORD':'數(shù)據(jù)庫密碼',
'HOST':'ip地址',
'PORT':'端口'
}
}
12:打開數(shù)據(jù)庫新增上面配置的數(shù)據(jù)庫名(DATABASE1)
13:配置model,進(jìn)入app目錄的models.py文件
import time
from django.db import models
# Create your models here.
class Test(models.Model):
str = models.CharField("字符串", max_length=30, null=True, blank=True)
num = models.IntegerField("數(shù)字", default=1, null=True, blank=True)
create_time = models.DateTimeField("時(shí)間", default=time.strftime('%Y-%m-%d %H:%M:%S'), blank=True)
14:遷移數(shù)據(jù)至數(shù)據(jù)庫
python manage.py makemigrations
python manage.py migrate
15:這時(shí)候大概率會(huì)報(bào)錯(cuò)如下:
Traceback (most recent call last):
中間省略若干
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/MySQLdb/__init__.py", line 24, in module>
version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined
此報(bào)錯(cuò)主要因?yàn)镸ysqldb 不兼容 python3.5 以后的版本
16:修改項(xiàng)目目錄下的__init_.py,增加以下代碼
import pymysql
pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()
好啦 至此常用的都配置完成了
到此這篇關(guān)于Django創(chuàng)建項(xiàng)目+連通mysql的操作方法的文章就介紹到這了,更多相關(guān)Django創(chuàng)建項(xiàng)目連通mysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 在Django下創(chuàng)建項(xiàng)目以及設(shè)置settings.py教程
- Django框架創(chuàng)建項(xiàng)目的方法入門教程
- Django在Win7下的安裝及創(chuàng)建項(xiàng)目hello word簡明教程
- Python+Django+MySQL實(shí)現(xiàn)基于Web版的增刪改查的示例代碼
- django連接mysql數(shù)據(jù)庫及建表操作實(shí)例詳解
- python+Django+pycharm+mysql 搭建首個(gè)web項(xiàng)目詳解