django self.request.files.getList

发布于 2025-01-29 14:10:29 字数 1020 浏览 0 评论 0原文

因此,我已将输入的名称属性更改为一些自定义名称,但是视图调用 form_invalid 方法。为什么我的表格没有保存(验证)?

html:

<tr>
      <th><label for="id_photo">Image:</label></th>
      <td>
      <input type="file" name="custom_photo_name" accept="image/*" required id="id_photo" multiple>
      </td>
</tr>

表单:

class DateForm(forms.ModelForm):
    photo = forms.ImageField(required=False)
    
    class Meta:
        model = Date
        exclude = ('user',)

视图:

class UpdateDateView(LoginRequiredMixin, UpdateView):
    model = Date
    form_class = DateForm
    template_name = 'app/form/date_edit_form.html'

    @transaction.atomic
    def form_valid(self, form):
        date = form.save()
        self.request.FILES.getlist('custom_photo_name') # this returns an empty list []
        return super().form_valid(form)

self.request.files不根据 name 属性来获得值?为什么我不能联系我的文件?

So I have changed the input's name attribute to some custom name, but the view calls form_invalid method. Why my Form isn't saving (validating)?

html:

<tr>
      <th><label for="id_photo">Image:</label></th>
      <td>
      <input type="file" name="custom_photo_name" accept="image/*" required id="id_photo" multiple>
      </td>
</tr>

the form:

class DateForm(forms.ModelForm):
    photo = forms.ImageField(required=False)
    
    class Meta:
        model = Date
        exclude = ('user',)

the view:

class UpdateDateView(LoginRequiredMixin, UpdateView):
    model = Date
    form_class = DateForm
    template_name = 'app/form/date_edit_form.html'

    @transaction.atomic
    def form_valid(self, form):
        date = form.save()
        self.request.FILES.getlist('custom_photo_name') # this returns an empty list []
        return super().form_valid(form)

Doesn't the self.request.FILES takes the values according to name attribute? Why can't I reach my files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文