<i id='JBfQl'><tr id='JBfQl'><dt id='JBfQl'><q id='JBfQl'><span id='JBfQl'><b id='JBfQl'><form id='JBfQl'><ins id='JBfQl'></ins><ul id='JBfQl'></ul><sub id='JBfQl'></sub></form><legend id='JBfQl'></legend><bdo id='JBfQl'><pre id='JBfQl'><center id='JBfQl'></center></pre></bdo></b><th id='JBfQl'></th></span></q></dt></tr></i><div id='JBfQl'><tfoot id='JBfQl'></tfoot><dl id='JBfQl'><fieldset id='JBfQl'></fieldset></dl></div>

<small id='JBfQl'></small><noframes id='JBfQl'>

        • <bdo id='JBfQl'></bdo><ul id='JBfQl'></ul>
        <tfoot id='JBfQl'></tfoot><legend id='JBfQl'><style id='JBfQl'><dir id='JBfQl'><q id='JBfQl'></q></dir></style></legend>

        Django 文件上传和重命名

        时间:2023-08-30

          <tbody id='ScnD6'></tbody>
      1. <small id='ScnD6'></small><noframes id='ScnD6'>

          <legend id='ScnD6'><style id='ScnD6'><dir id='ScnD6'><q id='ScnD6'></q></dir></style></legend>
              <bdo id='ScnD6'></bdo><ul id='ScnD6'></ul>
              <tfoot id='ScnD6'></tfoot>

                  <i id='ScnD6'><tr id='ScnD6'><dt id='ScnD6'><q id='ScnD6'><span id='ScnD6'><b id='ScnD6'><form id='ScnD6'><ins id='ScnD6'></ins><ul id='ScnD6'></ul><sub id='ScnD6'></sub></form><legend id='ScnD6'></legend><bdo id='ScnD6'><pre id='ScnD6'><center id='ScnD6'></center></pre></bdo></b><th id='ScnD6'></th></span></q></dt></tr></i><div id='ScnD6'><tfoot id='ScnD6'></tfoot><dl id='ScnD6'><fieldset id='ScnD6'></fieldset></dl></div>

                  本文介绍了Django 文件上传和重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  用户正在上传特定问题的 .c 文件.我希望将文件重命名为userid_questionid.c"

                  User is uploading a .c file of a particular question. I want the file to be renamed as 'userid_questionid.c'

                  我的 models.py 是:

                  My models.py is :

                  from django.db import models
                  
                  class users(models.Model):
                      username = models.CharField(max_length=20)
                      password = models.CharField(max_length=20)
                      score=models.IntegerField(max_length=3)
                      def __unicode__(self):
                          return self.username
                  
                  class questions(models.Model):
                      question = models.TextField(max_length=2000)
                      qid=models.IntegerField(max_length=2)
                      def __unicode__(self):
                         return self.qid
                  
                  def content_file_name(instance, filename):
                      return '/'.join(['uploads', instance.questid.qid, filename])
                  
                  
                  class submission(models.Model):
                      user = models.ForeignKey(users)
                      questid = models.ForeignKey(questions)
                      file = models.FileField(upload_to=content_file_name)
                  

                  我试过这个.但它只是创建用户文件夹并将文件保存在其中.请帮忙.谢谢.我需要重命名文件.

                  I tried this. But it just creates the folder of user and saves file in it. Please help. Thank You. I need the file to be renamed.

                  推荐答案

                  你只需要改变你的 content_file_name 函数.下面的函数将创建如下路径:uploads/42_100.c,其中42 是用户的ID,100 是问题的ID.

                  You just need to change your content_file_name function. The function below will create paths like so: uploads/42_100.c, where 42 is the user's id, and 100 is the question's id.

                  import os
                  def content_file_name(instance, filename):
                      ext = filename.split('.')[-1]
                      filename = "%s_%s.%s" % (instance.user.id, instance.questid.id, ext)
                      return os.path.join('uploads', filename)
                  

                  这篇关于Django 文件上传和重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:模型中的外键(用户) 下一篇:删除未级联到 sqlalchemy 中的表

                  相关文章

                  <legend id='1fIWO'><style id='1fIWO'><dir id='1fIWO'><q id='1fIWO'></q></dir></style></legend>

                  <tfoot id='1fIWO'></tfoot>
                1. <small id='1fIWO'></small><noframes id='1fIWO'>

                      <i id='1fIWO'><tr id='1fIWO'><dt id='1fIWO'><q id='1fIWO'><span id='1fIWO'><b id='1fIWO'><form id='1fIWO'><ins id='1fIWO'></ins><ul id='1fIWO'></ul><sub id='1fIWO'></sub></form><legend id='1fIWO'></legend><bdo id='1fIWO'><pre id='1fIWO'><center id='1fIWO'></center></pre></bdo></b><th id='1fIWO'></th></span></q></dt></tr></i><div id='1fIWO'><tfoot id='1fIWO'></tfoot><dl id='1fIWO'><fieldset id='1fIWO'></fieldset></dl></div>
                      • <bdo id='1fIWO'></bdo><ul id='1fIWO'></ul>