如何将文件上载到同一型号但不同的字段(Django)

2024-04-25 01:34:43 发布

您现在位置:Python中文网/ 问答频道 /正文

在html页面上,每个文件都有多个上载按钮,但我需要每个用户将其上载到不同的模型字段

HTML

{% if user.userextended.chapter == '33' %}
    <table style="width:100%">
    <caption></caption>
      <tr>
        <th>Chapter 33 Benefits</th>
        <th>Checklist</th> 
        <th>Upload</th>
      </tr>
      <tr>
        <td>Concise Student Schedule</td>
        <td>NO</td> 
        <td><form method="post" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form.as_p }}
            <button type="submit">Upload</button>
            </form>
        </td>
      </tr>
      <tr>
        <td> Audit</td>
        <td>NO</td> 
        <td><form method="post" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form.as_p }}
            <button type="submit">Upload</button>
            </form>
        </td>
      </tr>
    </table>

等等

Models.py

class UserExtended(models.Model):
    #fields for checking 
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    chapter = models.CharField(max_length=4,choices=[('33','33'), ('30','30'), ('31','31'),('35','35'),('1606','1606')],blank=True)

#saves file to a userid directory
def user_directory_path(instance, filename):
    return 'students/user_{0}/{1}'.format(instance.user.id,filename)

    Conc_stud_sched = models.FileField(upload_to=user_directory_path,validators=[FileExtensionValidator(allowed_extensions=['pdf'])],blank=True,null = True)
    star_deg_audit = models.FileField(upload_to=user_directory_path,validators=[FileExtensionValidator(allowed_extensions=['pdf'])],blank=True,null = True)

等等

我需要一些帮助来编写此任务的视图和表单

谢谢


Tags: topathformtruemodelstablebuttondirectory