第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 Flask-WTF 中填充表單和選擇默認(rèn)值

在 Flask-WTF 中填充表單和選擇默認(rèn)值

qq_遁去的一_1 2023-06-20 13:34:23
我有一個(gè)用于撰寫和編輯博客文章的表單,如下所示:class EditorForm(FlaskForm):    title = StringField('Title', validators=[DataRequired(), Length(min=1, max=250)])    body = PageDownField('Body', validators=[DataRequired()])    tags = SelectMultipleField('Tags', coerce=int)    timestamp = DateTimeField('Timestamp')    published = BooleanField('Publish?')    update = BooleanField('Update Timestamp?')    delete = SubmitField('Delete')    submit = SubmitField('Save')在我看來,我區(qū)分編輯現(xiàn)有帖子和創(chuàng)建新帖子。對(duì)于現(xiàn)有的帖子,如果它們有關(guān)聯(lián)的標(biāo)簽,我希望它們?cè)诒韱沃型怀鲲@示,SelectMultipleField以便用戶可以看到它們。如果這些被突出顯示并且我想刪除標(biāo)簽,我需要能夠取消突出顯示它們并提交表單來這樣做。以下是我目前觀點(diǎn)的相關(guān)部分:@app.route('/editor/<slug>', methods=['GET', 'POST'])@app.route('/editor/', methods=['GET', 'POST'])@login_requireddef editor(slug=None):    # old post or new post?    if slug:        post = Post.query.filter_by(slug=slug).first()    else:        post = Post()    # populate form, blank for new post    form = EditorForm(obj=post)        # populate tags field    form.tags.choices = [(tag.id, tag.tag) for tag in Tag.query.order_by('tag')]    # declare list for tag highlights on GET    if request.method == 'GET':        form.tags.default = []    # if post has linked tags, highlight them    if post.tags:        for tag in post.tags:            if tag.id not in form.tags.default:                form.tags.default.append(tag.id)        form.process()在解決與我的問題相關(guān)的其他問題時(shí),我發(fā)現(xiàn)我不能直接使用form.tags.data來突出顯示關(guān)聯(lián)的標(biāo)簽,因?yàn)檫@意味著該字段將忽略用戶在表單上的操作,即使正確的選擇將被突出顯示。這就是我使用form.tags.default.form.tags.default似乎可以突出顯示正確的標(biāo)簽,但form.process()會(huì)擦除由填寫的所有其他字段form = EditorForm(obj=post)。所以我的問題是:如何使用現(xiàn)有的帖子數(shù)據(jù)填充我的表單并在同一實(shí)例中突出顯示正確的標(biāo)簽?
查看完整描述

1 回答

?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

我似乎已經(jīng)通過這個(gè)問題獲得了我想要的結(jié)果。我原來的問題的一部分實(shí)際上與我沒有在我的問題中發(fā)布的視圖中的一些代碼有關(guān)。


在 上,我將(突出顯示的選項(xiàng))if form.validate_on_submit():中的標(biāo)簽附加到我的. 在帖子已經(jīng)附加標(biāo)簽的情況下,這意味著取消選擇字段中的默認(rèn)值確實(shí)會(huì)根據(jù)需要清空,但無論如何仍然有原始數(shù)據(jù),因此沒有變化。form.tags.datapost.tagsform.tags.datapost.tags


這是通過以下方式解決的:


# empty tags list then add highlighted choices

    post.tags = []

    for id in form.tags.data:

        t = Tag.query.filter_by(id=id).first()

        post.tags.append(t)

我還更改了填充表單的代碼,使其更簡單。我錯(cuò)了需要使用defaultover data(坦率地說,我不明白兩者之間的區(qū)別):


    # populate form, blank for new post

    form = EditorForm(obj=post)

    # populate tags field

    form.tags.choices = [(tag.id, tag.tag) for tag in Tag.query.order_by('tag')]

    # populate defaults only on GET otherwise user choice overidden

    if request.method == 'GET':

        # declare default (highlighted) tags list

        form.tags.data = []

        # if post has tags, highlight them

        if post.tags:

            for tag in post.tags:

                form.tags.data.append(tag.id)


查看完整回答
反對(duì) 回復(fù) 2023-06-20
  • 1 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)