Home > database >  How to Fetch and Publish the latest data and list data in django
How to Fetch and Publish the latest data and list data in django

Time:02-18

enter image description hereI am currently working on django , Need some help how to achive my below goal

I need to publish the latest data and list data in a web app .

Below is the set of steps i followed

Created the Model.py

import datetime from statistics import mode from django.db import models

Create your models here.

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 snippet

{% 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 >
    <a  href="#">MEDICARE</a>
  </nav>

{% for d in document  %}

 <td>{{d.title}}</td>

{% endfor %}

{% endblock %}

How can we render both latest data and list of data from a model class in django?I am clear about rendering the list data using listview . can someone help in understanding how to display the latest data from the list to the listview.html

Thanks,Sid

CodePudding user response:

Well you would go about getting your recent publish data in your listview like this:

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')

in your html template you can do something like this to render the latest 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 >
    <a  href="#">MEDICARE</a>
  </nav>

{% for doc in document  %}

 <td>{{doc.title}}</td>

{% endfor %}

{% endblock %}

CodePudding user response:

Just add ordering by id in descending order:

 class documentlist(ListView):
        template_name='app/document_list.html'
        model=documents
        context_object_name='document'

        ordering = ['-id']

CodePudding user response:

I have figured out the issue and this helped me resolve the issue.

 def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['latest_post'] = documents.objects.latest('update_date')
        return context

Thanks, SIdh

  • Related