Python Django模板只显示最后一个文件,而不是所有上载的文件
我希望在每次演讲中,用户能够上传多个文件,所以我创建了一个具有FileField的模型。所以在我的模板中,我希望每个讲座,上传到特定讲座的文件,都能显示在模板中。问题是只有最后上传的文件才会显示Python Django模板只显示最后一个文件,而不是所有上载的文件,python,django,Python,Django,我希望在每次演讲中,用户能够上传多个文件,所以我创建了一个具有FileField的模型。所以在我的模板中,我希望每个讲座,上传到特定讲座的文件,都能显示在模板中。问题是只有最后上传的文件才会显示 {%get_TEACH_category_显示为category_list%} 讲座 {类别列表%中的类别的百分比} {{category.grouper}} {category.list%中c的%s} ............. {{c.演讲题目} {{c.content}} {%if c
{%get_TEACH_category_显示为category_list%}
讲座
{类别列表%中的类别的百分比}
- {{category.grouper}}
{category.list%中c的%s}
.............
- {{c.演讲题目}
- {{c.content}}
{%if c.files%}
{%endif%}
{%endfor%}
{%endfor%}
def课程(请求,slug):
query=Course.objects.get(slug=slug)
上下文={'courses':Course.objects.filter(slug=slug),
“讲座”:查询.讲座.排序依据(“讲座类别”),
}
返回渲染(请求'courses/courses.html',上下文)
课堂讲座(models.Model):
课程=models.ForeignKey('course',on_delete=models.CASCADE,默认值='',相关课程名称='touchts')
讲座类别=模型.IntegerField(选项=((0,“类”),
(1,“研讨会”),
),默认值为0)
讲座题目=模型.CharField(最大长度=100)
content=models.TextField()
files=models.ForeignKey('FileUpload',on_delete=models.CASCADE,null=True,blank=True,)
定义(自我):
return str(自我讲座类别)
类文件上载(models.Model):
files=models.FileField(上传到class='documents',null=True,blank=True)
定义(自我):
返回str(self.files)
def文件链接(自):
如果self.xml文件:
返回“”%(self.files.url)
其他:
返回“无附件”
file\u link.allow\u tags=True
file\u link.short\u description='file Download'
您将外键
放错了位置。应该是相反的:
class FileUpload(models.Model):
uploaded_file = models.FileField(upload_to='documents', null=True, blank=True)
lecture = models.ForeignKey('Lecture', related_name='files')
现在,一堂课可以上传很多文件。然后,您可以在模板中迭代文件:
{% for file in c.files.all %}
<li><a href='{{ MEDIA_URL }}{{ file.upploaded_file.url }}'>download</a></li>
{% endfor %}
{%for c.files.all%中的文件]
{%endfor%}
希望有帮助 ForeignKey创建多对一关系。这意味着,现在你可以有许多相同图像的讲座,但不能有许多相同讲座的图像。 要解决此问题,可以使用多对多关系:
files = models.models.ManyToManyField('FileUpload')
在模板中:
{% for file in c.files.all %}
<li><a href='{{ MEDIA_URL }}{{ file.url }}'>download</a></li>
{% endif %}
{%for c.files.all%中的文件]
{%endif%}
同样,你也可以像Jahongir Rahmonov回答中那样,颠倒多对一的关系。这将允许为一次讲座添加多个文件 您的外键关系错误。根据您当前的模型,您的讲课表将仅引用某个特定讲课的上次上载文件 您的代码应该如下所示
#models.py
class Lecture(models.Model):
course = models.ForeignKey('Course', on_delete=models.CASCADE, default='', related_name='lectures')
lecture_category = models.IntegerField(choices=((0, "Classes "),
(1, "Seminars"),
), default=0)
lecture_title = models.CharField(max_length=100)
content = models.TextField()
def __str__(self):
return str(self.lecture_category)
class FileUpload(models.Model):
file = models.FileField(upload_to='documents', null=True, blank=True)
lecture = models.ForeignKey('Lecture', related_name='files')
def __str__(self):
return str(self.file)
然后在模板中,您可以执行以下操作
{% for file in c.file_set.all %}
<li><a href='{{ MEDIA_URL }}{{ file.url }}'>download</a></li>
{% endif %}
{%for c.file\u set.all%}
{%endif%}
另外,如果您想直接从课堂模型上传文件,您可以使用在admin.py中自定义ModelAdmin类。谢谢,这就解决了问题!还有一个问题,当我保存讲座时,它会说“讲座0已保存!”。我认为这是选择领域的应有之义。如何更改?@Daniel更改您的
\uuuu str\uuuu
方法以获得更有意义的内容。如果我删除str,则str返回非字符串(int类型)@Daniel我想您误解了。您想将“讲座0已保存!”更改为什么?它应该说什么?是的,你是对的。我返回了错误的属性,而不是标题,我返回的是类别。一切都解决了,谢谢!问题解决了,但有人投了反对票。。。。