如何在django中获取并发布最新数据和列表数据
How to Fetch and Publish the latest data and list data in django
我目前正在研究 django,需要一些帮助来实现我的以下目标
我需要在网络应用程序中发布最新数据和列表数据。
以下是我遵循的一组步骤
创建了 Model.py
导入日期时间
从统计导入模式
来自 django.db 导入模型
在此处创建您的模型。
class documents(models.Model):
author= models.CharField(max_length=30)
title=models.CharField(max_length=50)
description=models.TextField()
creation_date=models.DateTimeField()
update_date=models.DateTimeField()
View.py
from django.shortcuts import render
from django.views.generic.list import ListView
from .models import documents
# Create your views here.
class documentlist(ListView):
template_name='app/document_list.html'
model=documents
context_object_name='document'
HTML 片段
{% extends 'base.html' %}
{% block title %} MY HOMEPAGE {% endblock %}
{% block css %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
{% endblock %}
{% block content %}
<nav class=" navbar navbar-dark bg-primary">
<a class="navbar-brand mb-0 h1" href="#">MEDICARE</a>
</nav>
{% for d in document %}
<td>{{d.title}}</td>
{% endfor %}
{% endblock %}
我们如何在 django 中呈现来自模型 class 的最新数据和数据列表?我很清楚使用 listview 呈现列表数据。有人可以帮助理解如何将列表中的最新数据显示到 listview.html
谢谢,Sid
只需按id降序添加排序即可:
class documentlist(ListView):
template_name='app/document_list.html'
model=documents
context_object_name='document'
ordering = ['-id']
好吧,您可以像这样在列表视图中获取最近发布的数据:
class documentlist(ListView):
template_name='app/document_list.html'
model=documents
context_object_name='document'
def get_queryset(self):
return documents.objects.filter(author=request.user).order_by('creation_date')
在您的 html 模板中,您可以执行类似的操作来呈现最新的 post
% extends 'base.html' %}
{% block title %} MY HOMEPAGE {% endblock %}
{% block css %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
{% endblock %}
{% block content %}
<nav class=" navbar navbar-dark bg-primary">
<a class="navbar-brand mb-0 h1" href="#">MEDICARE</a>
</nav>
{% for doc in document %}
<td>{{doc.title}}</td>
{% endfor %}
{% endblock %}
我已经找到问题所在,这帮助我解决了问题。
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['latest_post'] = documents.objects.latest('update_date')
return context
谢谢,
SIdh
我需要在网络应用程序中发布最新数据和列表数据。
以下是我遵循的一组步骤
创建了 Model.py
导入日期时间 从统计导入模式 来自 django.db 导入模型
在此处创建您的模型。
class documents(models.Model):
author= models.CharField(max_length=30)
title=models.CharField(max_length=50)
description=models.TextField()
creation_date=models.DateTimeField()
update_date=models.DateTimeField()
View.py
from django.shortcuts import render
from django.views.generic.list import ListView
from .models import documents
# Create your views here.
class documentlist(ListView):
template_name='app/document_list.html'
model=documents
context_object_name='document'
HTML 片段
{% extends 'base.html' %}
{% block title %} MY HOMEPAGE {% endblock %}
{% block css %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
{% endblock %}
{% block content %}
<nav class=" navbar navbar-dark bg-primary">
<a class="navbar-brand mb-0 h1" href="#">MEDICARE</a>
</nav>
{% for d in document %}
<td>{{d.title}}</td>
{% endfor %}
{% endblock %}
我们如何在 django 中呈现来自模型 class 的最新数据和数据列表?我很清楚使用 listview 呈现列表数据。有人可以帮助理解如何将列表中的最新数据显示到 listview.html
谢谢,Sid
只需按id降序添加排序即可:
class documentlist(ListView):
template_name='app/document_list.html'
model=documents
context_object_name='document'
ordering = ['-id']
好吧,您可以像这样在列表视图中获取最近发布的数据:
class documentlist(ListView):
template_name='app/document_list.html'
model=documents
context_object_name='document'
def get_queryset(self):
return documents.objects.filter(author=request.user).order_by('creation_date')
在您的 html 模板中,您可以执行类似的操作来呈现最新的 post
% extends 'base.html' %}
{% block title %} MY HOMEPAGE {% endblock %}
{% block css %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
{% endblock %}
{% block content %}
<nav class=" navbar navbar-dark bg-primary">
<a class="navbar-brand mb-0 h1" href="#">MEDICARE</a>
</nav>
{% for doc in document %}
<td>{{doc.title}}</td>
{% endfor %}
{% endblock %}
我已经找到问题所在,这帮助我解决了问题。
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['latest_post'] = documents.objects.latest('update_date')
return context
谢谢, SIdh