溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

django配置mysql數(shù)據(jù)庫

發(fā)布時(shí)間:2020-07-14 07:41:04 來源:網(wǎng)絡(luò) 閱讀:749 作者:王家東哥 欄目:數(shù)據(jù)庫

查看ubuntu已安裝的所有軟件:

dpkg?-l??
dpkg?-l?|?grep?mysql


查看軟件安裝的路徑

dpkg?-L?|?grep?mysql


查看開機(jī)啟動(dòng)的軟件,需要額外安裝插件:

sudo?apt-get?install?rcconf
rcconf

更能全一點(diǎn)的:
sudo?apt-get?install?sysv-rc-conf
sysv-rc-conf


安裝mysql:

#?apt-get?install?python-setuptools?libmysqld-dev?libmysqlclient-dev?
#?easy_install?mysql-python
或者?#pip?install?mysql-python



django setting配置:

DATABASES?=?{
????'default':?{
????????'ENGINE':?'django.db.backends.mysql',?
????????'NAME':?'books',??#你的數(shù)據(jù)庫名稱
????????'USER':?'root',??#你的數(shù)據(jù)庫用戶名
????????'PASSWORD':?'',???#你的數(shù)據(jù)庫密碼
????????'HOST':?'',???????#你的數(shù)據(jù)庫主機(jī),留空默認(rèn)為localhost
????????'PORT':?'3306',???#你的數(shù)據(jù)庫端口
????}
}


在model模塊中添加如下建表語句:

vi app/models.py

class?test1(models.Model):????
????name?=?models.CharField(max_length=20)???#定義的字段name為字段名

model模塊在app中,其中定義的類名就是表名(但是在數(shù)據(jù)庫中的表名會(huì)以app為前綴,例如項(xiàng)目為app則實(shí)際創(chuàng)建的表名為app_test1),CharField相當(dāng)于varchar,DateField相當(dāng)于datetime,max_length 相當(dāng)于參數(shù)限定長度“varchar(20)”


python?manage.py?makemigrations????#查看表有哪些更改
python?manage.py?migrate?app?????#創(chuàng)建表結(jié)構(gòu)

注意:這里如果之前已經(jīng)同步過一次數(shù)據(jù),現(xiàn)在又要添加字段,會(huì)報(bào)錯(cuò),解決辦法是在字段后面添加

null=True

例如:

ages=models.CharField(max_length=10,null=True)



為表添加數(shù)據(jù):django需要查詢或者更新表時(shí),需先導(dǎo)入表名才能獲取表內(nèi)的數(shù)據(jù)。

from?app.models?import?test1
def?huoqu(request):????
???a?=?test1(name='wangjiadongge')?????#test1為表名,name為字段名。
???a.save()
???return?HttpResponse("數(shù)據(jù)添加成功!")



數(shù)據(jù)操作:

#獲取數(shù)據(jù)
def?huoqu(request):??
????
????#通過objects這個(gè)模型管理器的all()獲得所有數(shù)據(jù)行,相當(dāng)于SQL中的SELECT?*?FROM
????list?=?test1.objects.all()????
????
????#filter相當(dāng)于SQL中的WHERE,可設(shè)置條件過濾結(jié)果
????list1?=?test1.objects.filter(id=1)?
????
????#獲取單個(gè)對(duì)象
????list2?=?test.objects.get(id=1)?
????
????#限制返回的數(shù)據(jù)?相當(dāng)于?SQL?中的?OFFSET?0?LIMIT?2;
????test1.objects.order_by('name')[0:2]
????
????#數(shù)據(jù)排序
????test1.objects.order_by("id")
????
????#上面的方法可以連鎖使用
????test1.objects.filter(name="runoob").order_by("id")
????
????#輸出所有數(shù)據(jù)
????for?var?in?list:????????response1?+=?var.name?+?"?"
????response?=?response1
????return?HttpResponse("<p>"?+?response?+?"</p>")

#更新數(shù)據(jù)
#?-*-?coding:?utf-8?-*-
?from?django.http?import?HttpResponse
?from?app.models?import?test1
?def?testdb(request):????#修改其中一個(gè)id=1的name字段,再save,相當(dāng)于SQL中的UPDATE
????test?=?test1.objects.get(id=1)
????test.name?=?'Google'
????test.save()
????
????#另外一種方式
????#test1.objects.filter(id=1).update(name='Google')
????
????#?修改所有的列
????#?test1.objects.all().update(name='Google')
????
????return?HttpResponse("<p>修改成功</p>")
#刪除數(shù)據(jù)
#?-*-?coding:?utf-8?-*-
?from?django.http?import?HttpResponse
?from?app.models?import?Test
?#?數(shù)據(jù)庫操作def?testdb(request):????#?刪除id=1的數(shù)據(jù)
????test1?=?Test.objects.get(id=1)
????test1.delete()
????
????#另外一種方式
????#test1.objects.filter(id=1).delete()
????
????#刪除所有數(shù)據(jù)
????#test1.objects.all().delete()
????return?HttpResponse("<p>刪除成功</p>")


#django在前端中展示從數(shù)據(jù)庫中獲取到的數(shù)據(jù):


html:

{%?for?a?in?names?%}
????id={{?a.id?}}:name={{?a.name?}}:sex={{?a.sex?}}
{%?endfor?%}


注意:這里展示的數(shù)據(jù)必須是單條數(shù)據(jù),若是展示整個(gè)數(shù)據(jù)庫的內(nèi)容必須是逐條,整個(gè)取的話會(huì)導(dǎo)致出現(xiàn)QuerySet [<test2: test2 object>這種數(shù)據(jù)。



django:

def?testdb(request):
???
????#list?=?test2.objects.all()
????names?=?test2.objects.filter(id=1)
????print?names
????#return?HttpResponse('this?is?test?select?mysql!')
????return?render_to_response('a.html',locals())??#locals()是獲取整個(gè)本地變量


-------------------分割線--------------------



#django經(jīng)典例子:

from?django.db?import?models????????#導(dǎo)入models模塊

#表名:
class?publisher(models.Model):????????#定義表名為publish
????name=models.CharField(max_length=30)?????#表字段name
????address=models.CharField(max_length=50)??#表字段address
????city=models.CharField(max_length=60)?????#表字段city
????state_province=models.CharField(max_length=30)???
????county=models.CharField(default="CN",max_length=50)
????website=models.URLField()??????????#表字段website,字段類型為地址

表名:
class?author(models.Model):
????first_name=models.CharField(max_length=30)
????last_name=models.CharField(max_length=40)
????email=models.EmailField(blank=True)?????#字段名字為email,字段類型為email

#表名:
class?book(models.Model):
????title=models.CharField(max_length=100)?????#字段名為title,字段類型為vachar
????authors=models.ManyToManyField(author)?????#字段名為author,字段類型為ManyToManyField
????publisher=models.ForeignKey(publisher)?????#關(guān)聯(lián)外部表publisher
????publication_date=models.DateField()????????#字段名為publication_date,類型為日期類型


python?manage.py?makemigrations?????#查看表有哪些更改
python?manage.py?migrate?????#同步數(shù)據(jù)庫
#運(yùn)行上面這條命令出現(xiàn)的錯(cuò)誤:
#Apply?all?migrations:?admin,?app,?auth,?contenttypes,?sessions
#Running?migrations:
#解決:這個(gè)錯(cuò)誤是因?yàn)橐呀?jīng)同步過一次數(shù)據(jù)庫引起的,如果表中的字段要增加,需要添加null=True
#比如:ages=models.CharField(max_length=10,null=True)
#如果是新建一個(gè)表,則要?jiǎng)h除app中migrations文件夾(一般不需要)。


#如果需要用戶和密碼,則執(zhí)行:

python?manage.py?createsuperuser???#創(chuàng)建用戶
python?manage.py?changepassword????#更改密碼



#練習(xí)在python交互模式下操作數(shù)據(jù)庫:

./manage.py?shell??????????????#進(jìn)入django變量的交互器
from?app.models?import?publisher?#導(dǎo)入publisher數(shù)據(jù)庫。


#插入一條數(shù)據(jù):

p1=publisher(name='qinghua?university',address='wudaokou',city='beijing',state_province='beijing',county='china',website='www.qinghua.com')

p1.name ? ?#查看插入的name

p1.address ?#查看插入的address

p1.save() ? #插入的數(shù)據(jù)寫入數(shù)據(jù)庫中


#更新一條數(shù)據(jù):

p1.address="qinghualu"
p1.save()



#查看所有的數(shù)據(jù)

在models模塊中,建表語句下面添加如下:

def?__unicode__(self):
????return?self.name,self.address

然后再去交換窗口查看所有數(shù)據(jù):

publisher.objects.all()


#查詢國家等于中國的一條數(shù)據(jù):

publisher.objects.filter(country="china")


#查詢出來的數(shù)據(jù)進(jìn)行更改:

a=publisher.objects.get(name="beijing")
a.county="USA"
a.save()


#高效的更新數(shù)據(jù)方式,并且無需save:

publisher.objects.filter(id=1).update(name="qingdaodaxue")



#在瀏覽器中打開后臺(tái)管理數(shù)據(jù)庫界面:

http://192.168.110.106/admin/


賬號(hào)就是同步數(shù)據(jù)庫時(shí)創(chuàng)建的用戶名和密碼,登陸進(jìn)去。

在app下創(chuàng)建一個(gè)admin.py的文件

vi admin.py

from?django.contrib?import?admin
from?app.models?import?publisher,author,book
admin.site.register(publisher)
admin.site.register(author)
admin.site.register(book)

完成后,重新打開頁面。



#django中引用bootstrap:

在setting.py中:

MEDIA_ROOT='/root/project/statics/bootstrap/'


在url.py中:

from django.conf import settings

--------------------------------------分割線------------------------------------


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI