iT邦幫忙

0

django的表單一直說這個欄位是必須的

  • 分享至 

  • xImage

我用django弄了個表單,用來上傳字幕
https://ithelp.ithome.com.tw/upload/images/20230102/201480053DZUaFnrCv.jpg
但是不管怎麼樣就是過不了form.is_valid()
這個是forms.py

class UploadSubtitleForm(forms.Form):
    language = forms.ChoiceField(choices=(
        ("zh", "中文"),
        ("zh-Hans", "简体中文"),
        ("zh-Hant", "繁體中文"),
        ("en", "English"),
    ))
    subtitle = forms.FileField(label='字幕檔案', widget=forms.FileInput(attrs={'class': 'file-input', 'accept': '.vtt'}))

這個是views.py

@login_required()
def upload_subtitle(request):
    id = request.GET['id']
    if isowner(request.user.id, id):
        if request.method == 'POST':
            form = UploadSubtitleForm(request.POST, request.FILES)
            print(request.POST)
            if form.is_valid():
                cleaned_data = form.cleaned_data
                path = 'static/files/' + id + '/subtitles'
                if not os.path.isdir(path):
                    os.mkdir(path)
                with open(f"{path}/{cleaned_data['language']}.vtt", 'w', encoding='utf-8') as f:
                    f.write(request.FILES['subtitle'])

                return redirect('/player/?id=' + id)
            else:
                print(form.errors.as_data())  # here you print errors to terminal
        return render(request, 'html/siteone/add_subtitle.html', {'form': UploadSubtitleForm()})
    return redirect('/')

html

        <form id="form" method="post" class="center_items"
              style="flex-direction: column; width: 75vw; background: #fff; border-radius: 20px">
            <div id="form_inner" style="width: 80%">
                {% csrf_token %}
                <div class="fieldWrapper select">
                    <label for="{{ form.language.id_for_label }}">語言</label>
                    {{ form.language }}
                </div>
                <div style="display: flex;padding-top: 2.75em;">
                    <div class="file is-fullwidth" for="{{ form.subtitle.id_for_label }}">
                        <label class="file-label">
                            {{ form.subtitle }}
                            <span class="file-cta">
                                <span class="file-icon">
                                    <i class="fas fa-upload"></i>
                                </span>
                                <span class="file-label" style="width: 0;">
                                    上傳字幕檔...
                                </span>
                            </span>
                        </label>
                    </div>
                </div>
                <div>
                    <input class="button is-success is-fullwidth" value="上傳" type="submit">
                    <a class="button is-danger is-fullwidth" onclick="history.back()">取消</a>
                </div>
            </div>
        </form>

在終端機裡面有印出這些
https://ithelp.ithome.com.tw/upload/images/20230102/20148005hPbN3CYqPe.jpg
我確定我兩個欄位都有填好
我該怎麼修復他?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
froce
iT邦大師 1 級 ‧ 2023-01-02 17:01:14
最佳解答
subtitle = forms.FileField(label='字幕檔案', widget=forms.FileInput(attrs={'class': 'file-input', 'accept': '.vtt'}, required=False))

不行啊",如果required=False的話那這個表單就沒用處了呀!

froce iT邦大師 1 級 ‧ 2023-01-03 09:10:26 檢舉

喔,我知道你的問題了,很久沒用Django的form了,現在都是搞前後端分離。

因為你不是自動輸出整個form,所以你要上傳檔案,必須自己指定enctype

<form id="form" method="post" enctype="multipart/form-data" ...

謝謝

我要發表回答

立即登入回答