django 編寫數(shù)據(jù)接口
django-admin
•django-shell 新增文章太復(fù)雜•創(chuàng)建管理員用戶•登陸頁(yè)面進(jìn)行管理
創(chuàng)建超級(jí)用戶
Python manage.py createsuperuser
訪問(wèn):http://127.0.0.1:8000/admin/ admin / 1234qwer
注冊(cè)文章管理 到管理后臺(tái)
blogadmin.py
from django.contrib import admin
# Register your models here.
from .models import Article
admin.site.register(Article)
希望在列表 看到 文章標(biāo)題
blogmodels.py
# Create your models here.
class Article(models.Model):
article_id = models.AutoField(primary_key=True)
title = models.TextField()
brief_content = models.TextField()
content = models.TextField()
publish_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
接口獲取文章數(shù)據(jù)
獲取請(qǐng)求參數(shù)
# get
# 在項(xiàng)目下的urls.py下增加設(shè)置:
url(r'^user/$',views.index)
# 在user.views的index視圖中:
def index(request):
id = request.GET.get("id")
pid = request.GET.get("pid")
return HttpResponse("獲得數(shù)據(jù) %s %s"%(id,pid))
# post
def index(request):
id = request.POST.get("id")
pid = request.POST.get("pid")
return HttpResponse("獲得數(shù)據(jù) %s %s"%(id,pid))
獲取文章內(nèi)容
def article_content(request):
id = request.GET.get("id")
art = Article.objects.all()[int(id)]
return_str = 'article_id: %s,title: %s,brief_content: %s,content: %s,publish_date: %s' % (
art.article_id, art.title, art.brief_content, art.content, art.publish_date
)
return HttpResponse(return_str)
django 模板引擎
模板變量標(biāo)簽:{{ now }} 循環(huán)標(biāo)簽:{% for x in list %}, {% endfor %} 判斷標(biāo)簽:{% if %}, {% elseif %}, {% endif %}
添加頁(yè)面,渲染頁(yè)面
•添加 blogtemplatesblogarticle_list.html•blogviews.py 添加 頁(yè)面渲染方法
def get_index_page(request):
all_article = Article.objects.all()
blogtemplatesblogarticle_detail.html
return render(request, 'blog/article_list.html', {'article_list':all_article})
- blogurls.py 添加url
```python
from django.urls import path, include
import blog.views
urlpatterns = [
path('hello_world',blog.views.hello_world),
path(r'article',blog.views.article_content),
path(r'index',blog.views.get_index_page),
]
模型搜索介紹
模型的查詢 會(huì)創(chuàng)建 QuerySet 是惰性且唯一的
•all() 返回的 QuerySet 包含了數(shù)據(jù)表中所有的對(duì)象。•get() 檢索單個(gè)對(duì)象
鏈?zhǔn)竭^(guò)濾器
Entry.objects.filter(
headline__startswith='What'
).exclude(
pub_date__gte=datetime.date.today()
).filter(
pub_date__gte=datetime.date(2005, 1, 30)
)
條數(shù)限制
這會(huì)返回第 6 至第 10 個(gè)對(duì)象 (OFFSET 5 LIMIT 5):
Entry.objects.order_by('headline').all()[5:10]
•exact 匹配 iexact 大小寫不敏感 Entry.objects.get(headline__exact="Cat bites dog") # SELECT ... WHERE headline = 'Cat bites dog';•contains 包含 大小寫敏感 Entry.objects.get(headline__contains='Lennon') # SELECT ... WHERE headline LIKE '%Lennon%';•in Entry.objects.filter(id__in=[1, 3, 4])
配置靜態(tài)資源
文件存放
靜態(tài)文件放在對(duì)應(yīng)的 App 下的 static 文件夾中 或者 STATICFILES_DIRS 中的文件夾中。
measure/settings.py 配置
DEBUG=True
STATIC_URL = '/static/'
# 當(dāng)運(yùn)行 python manage.py collectstatic 的時(shí)候
# STATIC_ROOT 文件夾 是用來(lái)將所有STATICFILES_DIRS中所有文件夾中的文件,以及各app中static中的文件都復(fù)制過(guò)來(lái)
# 把這些文件放到一起是為了用Apache等部署的時(shí)候更方便
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
# 其它 存放靜態(tài)文件的文件夾,可以用來(lái)存放項(xiàng)目中公用的靜態(tài)文件,里面不能包含 STATIC_ROOT
# 如果不想用 STATICFILES_DIRS 可以不用,都放在 app 里的 static 中也可以
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "common_static"),
'/path/to/others/static/', # 用不到的時(shí)候可以不寫這一行
)
# 這個(gè)是默認(rèn)設(shè)置,Django 默認(rèn)會(huì)在 STATICFILES_DIRS中的文件夾 和 各app下的static文件夾中找文件
# 注意有先后順序,找到了就不再繼續(xù)找了
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
部署
python manage.py collectstatic
Nginx 配置
location /media {
alias /path/to/project/media;
}
location /static {
alias /path/to/project/collected_static;
}
References
[1] 更多系列文章在我博客: https://coderdao.github.io/






