zoukankan      html  css  js  c++  java
  • Django----博客文章数据返回

    步骤1:新建视图函数

    from django.shortcuts import render
    from django.http import HttpResponse;
    from blog.models import Article;
    
    # Create your views here.
    
    def hello_world(requestss):
        return HttpResponse('hello_world:zhongzhongzhong');
    
    
    #新建一个视图函数,返回博客文章
    def article_content(request):
        Aarticle=Article.objects.all()[0];--------->返回所有的文章对象,返回第一个文章
        title=Aarticle.title;----------->以下操作是取出这篇文章的所有需要展示的字段
        brief_conten=Aarticle.brief_content;
        content=Aarticle.content;
        article_id=Aarticle.article_id;
        publish_date=Aarticle.publish_date;
        return_str='title:%s,brief_content:%s,content:%s,publish_date:%s' %
                   (title,brief_conten,content,publish_date);
        return HttpResponse(return_str);

    步骤二:跟之前一样,配置路由

    ①、先配置应用层的路由

    urlpatterns=[
        path('hello_world',hello_world),
        path('content',article_content)
    ];

    ②、再配置项目层的路由

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('blog/',include('blog.blog_url'))------>因为跟之前的hello_world是在同一个应用项目内,所以不用再重复配置啦~~~
    ]

    步骤三:启动项目

    python manage.py runserver

    步骤四:在浏览器中打开,并输入路径

     

    ==============总结---------------------------

    截止目前

    返回了博客文章的数据,但是实际上并没有开始实现博客文章的任何功能.......但是截止目前,还是可以感受到Django的简易和强大哒

    继续学习

  • 相关阅读:
    pandas属性和方法
    os模块常用方法
    读/写xlsx文件
    读/写docx文件
    文件基本用法
    jieba.lcut方法
    移动端设置input属性disabled样式
    移动端日期选择,下拉框选择效果
    css背景色渐变代码
    拖动div元素
  • 原文地址:https://www.cnblogs.com/shenyexiaoqingxin/p/10623858.html
Copyright © 2011-2022 走看看