亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

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

python - Django 表單問題?

瀏覽:135日期: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():

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 狠狠做久久深爱婷婷97动漫 | 99视频在线免费 | 久久99亚洲网美利坚合众国 | 国产精品高清全国免费观看 | 久精品在线观看 | 欧美成人吃奶高清视频 | 国产精品成人亚洲 | 国产成人爱片免费观看视频 | 免费在线观看的毛片 | 在线免费一级片 | 国产精品久久久久久久免费大片 | 国产美女一级毛片 | 国产精品亚洲综合网站 | 亚洲一级黄色毛片 | 亚洲精品资源网在线观看 | 热re91久久精品国产91热 | 黄色作爱视频 | 欧美极品在线 | 亚洲日韩精品欧美一区二区 | 亚洲午夜精品一级在线播放放 | 久草资源在线观看 | 真人毛片免费全部播放完整 | 国产精品日韩欧美一区二区三区 | 国产高清片| 亚洲成人国产精品 | 欧美日韩国产亚洲一区二区三区 | 久久a 热6 | 日本道色 | 久久一日本道色综合久久m 久久伊人成人网 | 精品视频国产狼人视频 | 日韩美女一区 | 欧美在线一级视频 | 在线视频亚洲欧美 | 免费观看一级成人毛片 | 精品网址| 99精品在线免费观看 | 美国一级做a一级视频 | 一区二区三区四区在线播放 | 亚洲天堂视频网 | 国产午夜精品不卡视频 | 国产自约视频 |