網(wǎng)站首頁 編程語言 正文
1、創(chuàng)建項(xiàng)目
執(zhí)行代碼
django-admin startproject myweb
2、創(chuàng)建應(yīng)用
python manage.py startapp myapp
3、運(yùn)行項(xiàng)目
python manage.py runserver
如果沒有加端口號就是默認(rèn)8080,可以自己加端口號,防止與其它項(xiàng)目的端口號重復(fù)導(dǎo)致不能運(yùn)行成功
python manage.py runserver 0.0.0.0:8080
端口號可以隨意取,只要不重復(fù)就行
4、連接數(shù)據(jù)庫
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydemo',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
如果沒有安裝mysqlclient模塊執(zhí)行以下代碼進(jìn)行安裝
pip install mysqlclient
然后在目錄中創(chuàng)建模型
```python
from django.db import models
# Create your models here.
class Stu(models.Model):
'''自定義Stu表對應(yīng)的Model類'''
#定義屬性:默認(rèn)主鍵自增id字段可不寫
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=16)
age = models.SmallIntegerField()
sex = models.CharField(max_length=1)
classid=models.CharField(max_length=8)
# 定義默認(rèn)輸出格式
def __str__(self):
return "%d:%s:%d:%s:%s"%(self.id,self.name,self.age,self.sex,self.classid)
# 自定義對應(yīng)的表名,默認(rèn)表名:myapp_stu
class Meta:
db_table="stu"
激活模型——把新增的應(yīng)用添加到模型中。
編輯myweb/settings.py文件,并將該虛線路徑添加到該INSTALLED_APPS設(shè)置。
INSTALLED_APPS = [
'django.contrib.admin' ,
'django.contrib.auth' ,
'django.contrib.contenttypes' ,
'django.contrib.sessions' ,
'django.contrib.messages' ,
'django.contrib.staticfiles' ,
'myapp.apps.MyappConfig', #或者直接寫 myapp
]
5、創(chuàng)建數(shù)據(jù)庫stu
使用Navicat數(shù)據(jù)庫,在數(shù)據(jù)庫中要創(chuàng)建數(shù)據(jù)庫myweb,創(chuàng)建表stu,自己根據(jù)model模型中的數(shù)據(jù)類型增加數(shù)據(jù)。
數(shù)據(jù)都要有單引號,否則會報(bào)錯
錯誤記錄
TypeError: connect() argument 3 must be str, not int
類型不匹配的問題,要把數(shù)據(jù)庫中字段的類型跟代碼中的類型相匹配。
RuntimeError: Model class myapp.models.Stu doesn’t declare an explicit app_label and isn’t in an app
沒有激活模型,原來我是寫成myphoto,不是myapp,所以會報(bào)錯。
6、應(yīng)用
# 文件:myapp/views.py 文件代碼
from django.shortcuts import render
from django.http import HttpResponse
from myapp.models import Stu
# Create your views here.
def index(request):
return HttpResponse("Hello Django!")
def stu(request):
#獲取所有stu表信息
lists = Stu.objects.all()
print(lists)
#獲取單條學(xué)生信息
print(Stu.objects.get(id=1))
return HttpResponse("ok")
配置stu函數(shù)的訪問路由
#在myapp/urls.py文件中配置
path('stu/', views.stu),
然后啟動服務(wù)
python manage.py runserver
在網(wǎng)頁中的路徑改為以下路徑
http://127.0.0.1:8088/myapp/stu/
cmd里有數(shù)據(jù)顯示
顯示數(shù)據(jù)庫中的數(shù)據(jù)
7、總結(jié)
簡易的一個獲取數(shù)據(jù)庫內(nèi)容的例子,最主要的是要自己先創(chuàng)建數(shù)據(jù)才能獲取到。
原文鏈接:https://blog.csdn.net/qq_45802978/article/details/124328629
相關(guān)推薦
- 2024-07-18 redisson分布式鎖中waittime的設(shè)置
- 2022-12-05 通過sc命令獲得System權(quán)限的代碼_DOS/BAT
- 2023-01-07 SafeList?in?Flutter?and?Dart小技巧_Android
- 2023-01-28 一文詳解Go語言fmt標(biāo)準(zhǔn)庫的常用占位符使用_Golang
- 2023-05-06 docker?search命令的具體使用_docker
- 2022-01-26 iconv(): Detected an illegal character in input st
- 2022-07-19 react組件通訊的基本使用props
- 2022-09-24 Python?tkinter?列表框Listbox屬性詳情_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支