【发布时间】:2021-11-26 09:52:18
【问题描述】:
我试图在 django 中将数据从管理员动态添加到我的 HTML 页面,但是当我尝试在管理面板中添加数据时,它没有在 HTML 文件中显示任何内容。当我做makemigrations 时,我在下面得到这个
You are trying to add a non-nullable field 'link' to researchpaper without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option:
models.py
from django.db import models
class researchpaper(models.Model):
title = models.CharField(max_length=300)
publication = models.CharField(max_length=200)
link = models.CharField(max_length=300)
authors = models.CharField(max_length=200)
year = models.DateField("date published")
views.py
def publications(request):
data = researchpaper.objects.all()
return render(request, 'lab/publications.html',context={
"datas": data
})
urls.py
path('publications', views.publications, name='publications'),
HTML 文件
{% for public in datas %}
<tr>
<td>
<h5>2021</h5>
<p style="color: black;"><b>{{public.title}}</b></p>
<p style="font-size: 14px;"><i>{{public.publications}}</i></p>
<p style="font-size: 14px;"><i>{{public.author}}</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 %}
为什么会出现这个问题,请帮忙
【问题讨论】:
-
如果添加新字段,它必须允许 NULL 值或具有默认值。
-
分享模型
-
上面的模型代码我已经贴出来了
标签: python django django-models django-views django-urls