django项目中如何实现文件下载-后台上传-前台下载

1. 后台models.py代码

class CourseResource(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
    name = models.CharField(max_length=100, verbose_name="资源名")
    file = models.FileField(max_length=200, upload_to="course/resource/%Y/%m", verbose_name="下载地址")

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

2. 前端Html代码

<div class="box mb40">
    <h4>资料下载</h4>
    <ul class="downlist">

        {% for resource in course_resource %}
        <li>
            <span><i class="aui-iconfont aui-icon-file"></i>&nbsp;&nbsp;{{ resource.name }}</span>
            <a href="{{ MEDIA_URL }}{{ resource.file }}" class="downcode" target="_blank" download="" data-id="274" title="">下载</a>
        </li>
        {% endfor %}

    </ul>
</div>

 

上一篇:【1120 | Day63】序列化器的创建及声明


下一篇:Django+xadmin打造在线教育平台(四)