【发布时间】:2020-01-10 22:11:09
【问题描述】:
我正在使用 django 中的用户输入创建一个 docx 文件。现在,当单击提交按钮时,会生成下载链接,用户可以下载文件。但是,我想将文件保存在文件存储中并创建一个模板,用户可以在其中查看他过去创建的所有 docx 文件(或超链接)。 “有点像数据库”
models.py
class Timetables(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
default=1, related_name='timetables_files', null=True)
timetable_files = models.FileField(
null=True, blank=True, upload_to='word_documents')
views.py
def school_view(request):
if request.method == 'POST':
worddocument = docx.Document()
documenttitle = worddocument.add_heading(
school_name_view.title(), 0)
file = io.BytesIO()
worddocument.save(file)
response = HttpResponse(
content_type='application/vnd.openxmlformats-
officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename =quicktimetable.docx'
worddocument.save(response)
path = join(settings.MEDIA_ROOT, 'word_documents',
'quicktimetable.
file = open(path, 'w+b')
return response
class TimetablesView(ListView):
model = Timetables
template_name = 'quickmain/timetables_list.html'
timetables_list.html
{% if user.is_authenticated %}
{% for f in files %}
{{ f.files.url }}
{% endfor %}
{% for timetable_files in user.timetables_files.all %}
<embed src = "{{timetable_files.timetable_files.url}}" target="__blank">
{% endfor %}
{% else %}
如何保存生成的文件并在不同的模板上显示与该用户相关的所有文件。 我的 models.py 中有一个 FileField,但不知道如何使用它。每当用户要求时,我都会建议重新生成文件,但我也不知道。
【问题讨论】:
标签: python django django-models django-forms django-views