av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

python - Django 表單問題?

瀏覽:136日期:2022-07-16 17:34:23

問題描述

關(guān)于Django表單問題先給出models.py和forms.py

圖片描述python - Django 表單問題?

python - Django 表單問題?

再給出views.py的代碼

def articleUpdate(request, articleId):’’’Update the article instance: 1. Get the article to update; redirect to 404 if not found2. Render a bound form if the method is GET3. If the form is valid, save it to the model, otherwise render abound form with error messages’’’articleToUpdate = get_object_or_404( Article, id=articleId)template = ’article/articleCreateUpdate.html’if request.method == ’GET’: print(ArticleForm(instance=articleToUpdate)) articleForm = ArticleForm(instance=articleToUpdate) return render(request, template, {’articleForm’:articleForm, ’article’:articleToUpdate})# POSTarticleForm = ArticleForm(request.POST, instance=articleToUpdate)if not articleForm.is_valid(): return render(request, template, {’articleForm’:articleForm, ’article’:articleToUpdate})articleForm.save()messages.success(request, ’文章已修改’)return redirect(’article:articleRead’, articleId=articleId)def commentCreate(request, articleId): ’’’Create a new article instance1. If method is GET, render an empty form2 . If method is POST, perform form validation. Display error messages if the form is invalid3. Save the form to the model and redirect to the article page’’’template = ’article/commentCreate.html’ articleToUpdate = get_object_or_404( Article, id=articleId) if request.method == ’GET’:return render(request, template,{’commentForm’:CommentForm(), ’article’:articleToUpdate}) # POSTcommentForm = CommentForm(request.POST, instance=articleToUpdate) if not commentForm.is_valid():return render(request, template, {’commentForm’:commentForm(), ’article’:articleToUpdate}) commentForm.save() messages.success(request,’留言已新增’) return redirect(’article:articleRead’,articleId=articleId)

兩則方法是幾乎是一樣的,都是用forms表單,但是我使用的forms表單并不是同一類,一個是ArticleForm一個是CommentForm但是結(jié)果出現(xiàn)在views:commentCreate里它的效果是等于articleUpdate,即新增留言變成修改文章內(nèi)容python - Django 表單問題?

python - Django 表單問題?

問題解答

回答1:

表單是類,你取出數(shù)據(jù),為什么不填充到表單中。

else:form = CommentsForm(request.POST)if form.is_valid():

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 他揉捏她两乳不停呻吟动态图 | 男人天堂av网 | 毛片一区| av在线播放免费 | 一区二区三区在线免费 | 免费的黄色小视频 | 中文字幕一区二区三区视频 | 97福利视频 | 香蕉在线播放 | 日韩欧美中文在线 | 神马午夜嘿嘿 | 欧美久久综合 | 高清一区二区 | 日b免费视频 | 欧美成在线 | 欧美日韩一区二区三区 | 亚洲福利一区 | 日韩理论在线 | 一级黄片毛片 | 中文字幕免费看 | 国产黄色免费视频 | 日韩有码在线观看 | 日韩欧美在线视频 | www.日韩.com | 国产伦理一区二区 | 天天色网站 | 日韩福利在线 | 欧美专区在线 | 91成人免费版 | 久久久久久久国产精品 | 亚洲九九夜夜 | 五月婷婷中文字幕 | 懂色av色吟av夜夜嗨 | 夜夜操网站 | 亚洲黄色一级 | 免费毛片在线播放免费 | 涩久久 | 麻豆av在线 | 毛片免费播放 | 麻豆成人免费视频 | 中文在线观看免费视频 |