公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

django 编写数据接口

django-admin

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

创建超级用户

Python manage.py createsuperuser

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

注册文章管理 到管理后台

blogadmin.py

from django.contrib import admin


# Register your models here.
from .models import Article


admin.site.register(Article)

希望在列表 看到 文章标题

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

接口获取文章数据

获取请求参数

# 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 %}

添加页面,渲染页面

•添加 blogtemplatesblogarticle_list.html•blogviews.py 添加 页面渲染方法

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),
]

模型搜索介绍

模型的查询 会创建 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;
}

References

[1] 更多系列文章在我博客: https://coderdao.github.io/

分享到:
标签:django
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定