生活随笔
收集整理的這篇文章主要介紹了
Django环境配置
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Django安裝
#安裝最新版本的Django
$ pip install django
#或者指定安裝版本
pip install -v django==
1.7.1
- 項(xiàng)目創(chuàng)建
$ django-admin startproject my_blog - 建立Django app
$ python manage.py startapp article - 運(yùn)行程序
$ python manage.py runserver
pip安裝python模塊權(quán)限報(bào)錯(cuò)解決
windows下pip安裝python模塊時(shí)報(bào)錯(cuò)總結(jié) - 溫柔易淡 - 博客園
pycharm運(yùn)行django程序
配置: runserver 0.0.0.0:8000
參考:pycharm上運(yùn)行django服務(wù)器端、以及創(chuàng)建app方法
diango使用mysql數(shù)據(jù)庫(kù)
設(shè)置數(shù)據(jù)庫(kù)
Django項(xiàng)目建成后, 默認(rèn)設(shè)置了使用SQLite數(shù)據(jù)庫(kù), 在my_blog/my_blog/setting.py中可以查看和修改數(shù)據(jù)庫(kù)設(shè)置:
DATABASES = {
'default': {
'ENGINE':
'django.db.backends.mysql',
'NAME':
'csvt',
'USER':
'root',
'PASSWORD':
'root',
'HOST':
'',
'PORT':
'',}
}
同時(shí)要在init.py中寫(xiě)以下代碼
import pymysql
pymysql.install_as_MySQLdb()
pymysql的安裝
pip
install PyMySQL
創(chuàng)建models
在my_blog/article/models.py下編寫(xiě)如下程序:
from django.db
import models
class Article(models.Model) :title = models.CharField(max_length =
100) category = models.CharField(max_length =
50, blank =
True) date_time = models.DateTimeField(auto_now_add =
True) content = models.TextField(blank =
True, null =
True)
def __str__(self) :return self.title
class Meta: ordering = [
'-date_time']
其中str(self) 函數(shù)Article對(duì)象要怎么表示自己, 一般系統(tǒng)默認(rèn)使用 來(lái)表示對(duì)象, 通過(guò)這個(gè)函數(shù)可以告訴系統(tǒng)使用title字段來(lái)表示這個(gè)對(duì)象
- CharField 用于存儲(chǔ)字符串, max_length設(shè)置最大長(zhǎng)度
- TextField 用于存儲(chǔ)大量文本
- DateTimeField 用于存儲(chǔ)時(shí)間, auto_now_add設(shè)置True表示自動(dòng)設(shè)置對(duì)象增加時(shí)間
同步數(shù)據(jù)庫(kù)
$ python manage.py makemigrations
$ python manage.py migrate
Django Shell
現(xiàn)在我們進(jìn)入Django中的交互式shell來(lái)進(jìn)行數(shù)據(jù)庫(kù)的增刪改查等操作
$ python manage.py shell
Python
3.4.2 (v3
.4.2:ab2c023a9432, Oct
5 2014,
20:
42:
22)
[GCC
4.2.1 (Apple Inc. build
5666) (dot
3)] on darwin
Type
"help",
"copyright",
"credits" or "license" for more information.
(InteractiveConsole)
>>>
這里進(jìn)入Django的shell和python內(nèi)置的shell是非常類(lèi)似的
>>> from article.models
import Article
>>>
>>> Article.objects.create(title =
'Hello World', category =
'Python', content =
'我們來(lái)做一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)增加操作')
<Article: Article object>
>>> Article.objects.create(title =
'Django Blog學(xué)習(xí)', category =
'Python', content =
'Django簡(jiǎn)單博客教程')
<Article: Article object>
>>>
>>> Article.objects.all()
[<Article: Article object>, <Article: Article object>]
>>> Article.objects.get(id =
1)
<Article: Article object>
>>>
>>> first = Article.objects.get(id =
1)
>>> first.title
'Hello World'
>>> first.date_time
datetime.datetime(
2014,
12,
26,
13,
56,
48,
727425, tzinfo=<UTC>)
>>> first.content
'我們來(lái)做一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)增加操作'
>>> first.category
'Python'
>>> first.content =
'Hello World, How are you'
>>> first.content
'Hello World, How are you'>>>
>>> first.delete()
>>> Article.objects.all()
[<Article: Article object>] Blog.objects.all()
Blog.objects.filter(caption=
'blogname')
Blog.objects.filter(caption=
'blogname', id=
"1")
Blog.objects.filter(caption__contains=
'blogname')Blog.objects.get(caption=
'blogname')
Blog.objects.order_by(
"caption")
Blog.objects.order_by(
"-caption")
Blog.objects.order_by(
"caption",
"id")
Blog.objects.filter(caption__contains=
'blogname').order_by(
"-id")
Blog.objects.filter(caption__contains=
'blogname')[
0]
Blog.objects.filter(caption__contains=
'blogname')[
0:
3]
總結(jié)
以上是生活随笔為你收集整理的Django环境配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。