第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

django 編寫數(shù)據(jù)接口練習(xí)

標(biāo)簽:
Python 面試 Django

如果❤️我的文章有帮助,欢迎点赞、关注。这是对我继续技术创作最大的鼓励。[更多系列文章在我博客] coderdao.github.io/

django 编写数据接口练习

django-admin

  • django-shell 新增文章太复杂
  • 创建管理员用户
  • 登陆页面进行管理

创建超级用户

python manage.py createsuperuser

访问:http://127.0.0.1:8000/admin/
admin / 1234qwer

注册文章管理 到管理后台

blog\admin.py

from django.contrib import admin

# Register your models here.
from .models import Article

admin.site.register(Article)

希望在列表 看到 文章标题

blog\models.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

接口获取文章数据

获取请求参数

# get
# 在项目下的urls.py下增加设置:
url(r'^user/$',views.index)

# 在user.views的index视图中:
def index(request):
    id = request.GET.get("id")
    pid = request.GET.get("pid")
    return HttpResponse("获得数据 %s %s"%(id,pid))


# post 
def index(request):
    id = request.POST.get("id")
    pid = request.POST.get("pid")
    return HttpResponse("获得数据 %s %s"%(id,pid))

获取文章内容


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 模板引擎

模板变量标签:{{ now }}
循环标签:{% for x in list %}, {% endfor %}
判断标签:{% if %}, {% elseif %}, {% endif %}

添加页面,渲染页面

  • 添加 blog\templates\blog\article_list.html
  • blog\views.py 添加 页面渲染方法
def get_index_page(request):
all_article = Article.objects.all()

# blog\templates\blog\article_detail.html
return render(request, 'blog/article_list.html', {'article_list':all_article})
  • blog\urls.py 添加url
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),
]

模型搜索介绍

模型的查询 会创建 QuerySet 是惰性且唯一的

  • all() 返回的 QuerySet 包含了数据表中所有的对象。
  • get() 检索单个对象

链式过滤器

Entry.objects.filter(
    headline__startswith='What'
).exclude(
    pub_date__gte=datetime.date.today()
).filter(
    pub_date__gte=datetime.date(2005, 1, 30)
)

条数限制

这会返回第 6 至第 10 个对象 (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])

配置静态资源

文件存放

静态文件放在对应的 app 下的 static 文件夹中 或者 STATICFILES_DIRS 中的文件夹中。

measure/settings.py 配置

DEBUG=True

STATIC_URL = '/static/'
 
# 当运行 python manage.py collectstatic 的时候
# STATIC_ROOT 文件夹 是用来将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制过来
# 把这些文件放到一起是为了用apache等部署的时候更方便
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
 
# 其它 存放静态文件的文件夹,可以用来存放项目中公用的静态文件,里面不能包含 STATIC_ROOT
# 如果不想用 STATICFILES_DIRS 可以不用,都放在 app 里的 static 中也可以
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "common_static"),
    '/path/to/others/static/',  # 用不到的时候可以不写这一行
)
 
# 这个是默认设置,Django 默认会在 STATICFILES_DIRS中的文件夹 和 各app下的static文件夹中找文件
# 注意有先后顺序,找到了就不再继续找了
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;
}
點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消