【问题标题】:Save .docx file django for specific user为特定用户保存 .docx 文件 django
【发布时间】: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


    【解决方案1】:

    您必须创建文件和用户模型之间的关系。 例子 在您的模型中

    class DocxFile(models.Model):
     user = models.ForiegnKey(User)      #your user model name
     file = models.FileField('upload_to='/')
    

    那么在你看来

    from yourappname.models import DocxFile
        def docs_file(request):
        files = DocxFile.object.filter(user=request.user)
        return render('your template path', {'files':files})
    

    在您的模板中

    {% for f in files%}
    
    {{f.file.url}}
    
    {% endfor %} 
    

    【讨论】:

    • 感谢您的回复。我已经编辑了我的答案,并且使用我的模板,我可以通过从管理面板上传文件来查看特定用户应该存在的文件数量。我为不同的用户添加了不同数量的文件,我可以看到一些窗口,但在小窗口中它显示“找不到页面”,因为 django 更改了文件名,我不知道那里发生了什么。我现在真正遇到的唯一问题是将生成的 docx 文件保存在文件存储中。
    • 您可以简单地从 django 管理员上传文件。您必须在设置中定义 media_url 和 media_root。也可以从应用程序的前端上传。
    • docx 文件由用户输入生成。我已经尝试使用 File 和 ContentFile,但我无法找到一种方法来为当前登录的用户保存它。
    • 我找到了一种使用documentfile = Timetables() documentfile.timetable_files.name = path 保存文件的方法,但它不会为当前用户保存它,它会为id 1 的用户保存它,因为用户的默认值在Timetables() 模型中的外键是 1。
    • 那是因为你只保存文件路径也添加这个``` documentfile.user = request.user ```
    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2012-08-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多