Django 如何在模板上以人类可读的格式打印数据?

Django 如何在模板上以人类可读的格式打印数据?,django,Django,我有来自API的以下格式的数据:“2019-05-29” 我想在2019年5月29日打印出来 下面是我从API收到的文件和响应 API的响应 "Data": [ { "id": 1, "news_title": "Recent Update", "news_description": "This is recent one line descri

我有来自API的以下格式的数据:“2019-05-29” 我想在2019年5月29日打印出来 下面是我从API收到的文件和响应 API的响应

"Data": [
                {
                    "id": 1,
                    "news_title": "Recent Update",
                    "news_description": "This is recent one line description",
                    "image_id": 1,
                    "news_date": "2018-05-18",
                    "news_content": "the big content",
                    "slug": "recent-update",
                    "company_id": 1,
                    "user_id": 1
                }
            ]

Views.py:

def BlogViews(request,blog_type):
    """
        The blogs are displayed according to the latest, current-month and last-month classification
    """
    blog_type=blog_type.replace('-','_')
    response_blog=requests.get("API" % (settings.BASE_URL,blog_type),headers=headers,verify=False)
    if(response_blog.status_code==200):
        data_blog=response_blog.json()
        if(data_blog['ErrorCode']==0 and data_blog['SubErrorCode']==0):
            blog=BlogYearViews()
            if(len(data_blog['Data'])>0):
                blog_images=BlogImages(data_blog)
                blogs_and_images = zip(data_blog['Data'], blog_images)
                blog_type=blog_type.capitalize()
                blog_type=blog_type.replace('_',' ')
                return render(request,"CombinedBlog.html",{"blogs_and_images": blogs_and_images, "years":blog,"title":blog_type})
            else:
                blog_type=blog_type.capitalize()
                blog_type=blog_type.replace('_',' ')
                return render (request,"NoBlogs.html",{"title":blog_type,"years":blog})
        else:
            return redirect('/404')
CombinedBlog.html

<h2>{{title}} Articles</h2>
<ul>
{% for blog, image in blogs_and_images %}    
<li><h3>{{ blog.news_title }}</h3><br/>     
    <a href="/blog/article/{{ blog.slug }}"><img src="{{ image.image_name }}"/></a><br/>
    <time>{{blog.news_date}}</time><br/>
    <a href="/blog/article/{{ blog.slug }}">click here</a><br/></li>
{% endfor %}

{{title}}篇文章
    {%对于博客,博客中的图像\u和\u图像%}
  • {{blog.news_title}}

    {{blog.news\u date}}

  • {%endfor%}
Django模板过滤器具有您需要使用的功能

格式-
{{value}日期:“md,Y”}

月份上限(五月)-
M

日期(01-31)-
d

全年(2019年) -
Y

<h2>{{title}} Articles</h2>
<ul>
{% for blog, image in blogs_and_images %}    
<li><h3>{{ blog.news_title }}</h3><br/>     
    <a href="/blog/article/{{ blog.slug }}"><img src="{{ image.image_name }}"/></a><br/>
    <time>{{blog.news_date|date:"M d, Y" }}</time><br/>     # <----- use this
    <a href="/blog/article/{{ blog.slug }}">click here</a><br/></li>
{% endfor %}
{{title}}篇文章
    {%对于博客,博客中的图像\u和\u图像%}
  • {{blog.news_title}}


    {{blog.news_date|date:“md,Y”}
    #Django模板过滤器具有您需要使用的模板过滤器

    格式-
    {{value}日期:“md,Y”}

    月份上限(五月)-
    M

    日期(01-31)-
    d

    全年(2019年) -
    Y

    <h2>{{title}} Articles</h2>
    <ul>
    {% for blog, image in blogs_and_images %}    
    <li><h3>{{ blog.news_title }}</h3><br/>     
        <a href="/blog/article/{{ blog.slug }}"><img src="{{ image.image_name }}"/></a><br/>
        <time>{{blog.news_date|date:"M d, Y" }}</time><br/>     # <----- use this
        <a href="/blog/article/{{ blog.slug }}">click here</a><br/></li>
    {% endfor %}
    
    {{title}}篇文章
    
      {%对于博客,博客中的图像\u和\u图像%}
    • {{blog.news_title}}


      {{blog.news_date|date:“md,Y”}
      #打印日期的部分仍然是空的。您是否更早地获取了日期?是的,我更早地获取了格式为“2019-09-29”的数据您在模板中尝试什么?是,正如@exprator所说,您需要更改字符串date to date对象以使用此功能。打印日期的部分仍然为空。您是否更早地获取了日期?是,我更早地获取了格式为“2019-09-29”的数据您在模板中尝试什么?是,正如@exprator所说,您需要更改字符串date to date对象以使用此功能。