學(xué)習(xí)django
django 項(xiàng)目目錄介紹
manage.py
與項(xiàng)目進(jìn)行命令交互的命令行工具集的入口
項(xiàng)目管理器
wsgi.py
WSGI (python web server gateway interface)
python 服務(wù)器與網(wǎng)關(guān)接口
url.py
url配置文件
diango 項(xiàng)目中所有地址(頁(yè)面)都需要我們自己去配置其URL
setting.py
開啟項(xiàng)目
兩種方法
1偎痛、通過PyCharm創(chuàng)建 直接run
2、命令行創(chuàng)建 django-admin.py startproject projectName
啟動(dòng) python manage.py runsever
創(chuàng)建應(yīng)用
python manage.py startapp blog
然后在setting.py 中的INSTALLED_APPS添加應(yīng)用名
migrations 數(shù)據(jù)移植模塊
admin.py 該應(yīng)用的后臺(tái)管理系統(tǒng)配置
apps.py 該應(yīng)用的一些配置
models.py 數(shù)據(jù)模塊 摄咆,使用ORM框架
test.py 自動(dòng)化測(cè)試模塊
view.py 執(zhí)行響應(yīng)的代碼所在模塊,代碼邏輯處理的主要地點(diǎn)
創(chuàng)建第一個(gè)頁(yè)面(響應(yīng))
編輯blog.views
每個(gè)響應(yīng)對(duì)應(yīng)一個(gè)函數(shù)躬柬,函數(shù)必須返回一個(gè)響應(yīng)
函數(shù)必須存在一個(gè)參數(shù), 一般約定為request
每個(gè)響應(yīng)對(duì)應(yīng)一個(gè)URL
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World")
配置URL
編輯url.py
每一URL都已url的形式寫出來
url函數(shù)放在urlpatterns列表中
url函數(shù)三個(gè)參數(shù):URL(正則),對(duì)應(yīng)方法,名稱
import blog.views as bv
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', bv.index),
]
配置子URL 防止項(xiàng)目過大,url無法管理的問題
項(xiàng)目下的urls.py 增加代碼 url(r'^blog/', include('blog.urls'))
應(yīng)用下新建urls.py 文件,在urls.py中配置自己的url
例如:
urlpatterns = [
url(r'^index/$', views.index),
]
Templates
Html 文件
使用Django DTL語(yǔ)言
也可以使用第三方模板(如:Jinja2)
使用:
在App的根目錄下創(chuàng)建名叫Templates的目錄
在該目錄下創(chuàng)建HTML文件
在Views.py中返回render()
render 傳入三個(gè)參數(shù) request 听系、XXX.html、(字典)
html中用{{傳入的字典的key}}
如下所示
views.py
def index(request):
return render(request, 'index.html', {'haha': '你是狗'})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
你好<br/>
{{ haha }}
</body>
</html>
models
通常,一個(gè)Model對(duì)應(yīng)數(shù)據(jù)庫(kù)的一張數(shù)據(jù)表
Django中的Modles以類的形式表現(xiàn)
它包含一些基本字段以及數(shù)據(jù)的一些行為
創(chuàng)建數(shù)據(jù)表
class Article(models.Model):
title = models.CharField(max_length=32, default='Title')
content = models.TextField(null=True)
def __str__(self):
return self.title
生成數(shù)據(jù)表
命令行中進(jìn)入manage.py同級(jí)目錄
執(zhí)行 python manage.py makemigrations app名(可選)
在執(zhí)行 python manage.py migrate
查詢數(shù)據(jù)表生成sql語(yǔ)句
python manage.py sqlmigrate app名 0001
獲取數(shù)據(jù)庫(kù)中的數(shù)據(jù)
views.py 中import models
article = models.Article.object.get(pk=1)
HTML DTL語(yǔ)言
引用一般對(duì)象
{{ object }} ??????可以用 ?.?引用對(duì)象里面的屬性
for循環(huán)
{% for acticle in articles %}
{% endfor %}
Django中的超鏈接
超鏈接目標(biāo)地址
href后面是目標(biāo)地址
templates中可以用"{%url 'app_name:url_name' param%}"
<h1>
<a href="{% url 'blog:edit_page' 0%}">新文章</a>
</h1>
{% for article in articles %}
<h1><a href="{% url 'blog:article_page' article.id %}">{{ article.title }}</a></h1>
<h3>{{ article.content }}</h3>
<br/>
{% endfor %}
表單提交Post方法
<html>
<form action="{% url 'blog:edit_action' %}" method="POST">
<input type="text" name="title"/>
<input type="text" name="content"/>
<input type="submit" value="提交">
</form>
</html>
獲取請(qǐng)求參數(shù)
def edit_action(request):
# 獲取提交的參數(shù),并在數(shù)據(jù)庫(kù)中添加數(shù)據(jù)
title = request.POST.get('title', 'TITLE') # title = request.POST['title']
content = request.POST.get('content', 'CONTENT') # request.POST['content']