【问题标题】:Unable to update post using models in django无法使用 django 中的模型更新帖子
【发布时间】:2021-11-26 06:26:25
【问题描述】:

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

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文件

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

【问题讨论】:

    标签: python django django-models django-views django-admin


    【解决方案1】:

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

    {% for paper in researchposts %}
    ...
    

    【讨论】:

    • 好的,我已经改变了,但是,当我在管理面板中添加数据时,它没有反映在 html 页面中?
    • 刷新页面后? html页面上是否显示任何纸张数据?你在 html 页面上能看到什么?
    • 刷新后文本字段中的所有内容都消失了。
    【解决方案2】:

    将您的views.py更改为:

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

    在您的 html 文件 (lab/publications.html) 中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2018-12-04
      • 2013-08-06
      • 2019-05-20
      • 2019-04-15
      • 2018-04-13
      相关资源
      最近更新 更多