Home > Software design >  Unable to update post using models in django
Unable to update post using models in django

Time:10-07

I want to prepare a webpage which outputs book name, publisher and author and want to add the data dynamically using admin panel but unable to do it using below code. Please help

models.py

from django.db import models
class researchpaper(models.Model):
    title = models.CharField(max_length=200)
    publication = models.CharField(max_length=200)
    authors = models.CharField(max_length=200)

    def __str__(self) -> str:
        return self.title

views.py

def publications(request):
    context = {
            'researchposts': researchpaper.objects.all()
    }
    return render(request, 'lab/publications.html',context)

urls.py

path('publications', views.publications, name='publications'),

html file

 {% for paper in object_list %}
                <tr>
                    <td>
                        <h5>2021</h5>
                        <p style="color: black;"><b>{{paper.title}}1. A minimal model for synaptic integration in simple neurons</b></p>
                        <p style="font-size: 14px;"><i>Physica D: Nonlinear Phenomena, 2021</i></p>
                        <p style="font-size: 14px;"><i>Adrian Alva, Harjinder Singh.</i></p>
                    </td>
                    <td><a href="#"
                            target="blank" style="color: dodgerblue;"><i class="ai ai-google-scholar-square ai-2x"
                                style="padding-right: 10px;"></i></a></td>
                </tr>
                <tr>
                    <td>
                        <hr>
                    </td>
                </tr>
                {% endfor %}

CodePudding user response:

Should object_list in your template be researchposts since that's the key you provide in context?

{% for paper in researchposts %}
...

CodePudding user response:

Change your views.py as:

def publications(request):
    researchposts = researchpaper.objects.values('title', 'publications', 'author')
    return render(request, 'lab/publications.html', {'researchposts':researchposts})

In your html file (lab/publications.html):

{% for text in researchposts %}
Name: {{ text.title }} <br>
Pub: {{ text.publications }} <br>
Author: {{ text.author }} <br>
{% endfor %}
  • Related