首页 > 解决方案 > 无法使用 django 中的模型更新帖子

问题描述

我想准备一个输出书名、出版商和作者的网页,并希望使用管理面板动态添加数据,但无法使用下面的代码来完成。请帮忙

模型.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

视图.py

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

网址.py

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

.html 文件

 {% 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 %}

标签: pythondjangodjango-modelsdjango-viewsdjango-admin

解决方案


模板中的 object_list 是否应该是研究帖子,因为这是您在上下文中提供的关键?

{% for paper in researchposts %}
...

推荐阅读