好的,我试图显示我的FileField
数据库中的所有条目,并为每个条目分配一个html按钮。按下按钮时,将打开一个新窗口,显示存储在数据库中的pdf文件
型号.py
class UploadedFile(models.Model):
the_file = models.FileField()
def __str__(self):
return self.the_file.name[:50]
视图.py
def show_file(response,pk):
file_name=UploadedFile.objects.get(pk=pk)
pdf = open( 'chartsapp/%s' % str(file_name), 'rb')
response = FileResponse(pdf)
return response
class Showing_File(ListView):
model = UploadedFile
template_name = 'index.html'
context_object_name = 'the_uploaded_files_list'
url.py
path('show_file/<pk>', views.show_file),
index.html
{% for file in the_uploaded_files_list %}
<p> {{ file.the_file.name }} </p>
<input type="button" value="Show Report" onclick="window.open('show_file/{{file.pk}}')">
{% endfor %}
设置.py
MEDIA_URL = '/chartsapp/'
MEDIA_ROOT=os.path.join(BASE_DIR, "chartsapp")
发生的是加载了我的index.html,但是我没有看到页面上显示的数据库条目名称或相应的按钮。
我想Media_Url
和Media_root
路径可能把我搞砸了。我已经阅读了文档,并对路径进行了修改,但没有任何乐趣。
请看下面的项目布局图,以防万一是媒体的根源把它搞砸了
如果我错了,但当我在django admin中上传到FileField
时请更正。文件本身没有上载,数据库更多的是它将存储在的文件夹的路径。该路径应该是媒体根
目前没有回答
相关问题 更多 >
编程相关推荐