如何将文件上传到相同模型但不同字段(Django)

时间:2019-03-21 17:30:37

标签: python django forms view model

在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)

以此类推...

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

谢谢。

0 个答案:

没有答案