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

您的位置:首頁技術文章
文章詳情頁

python - Django 表單問題?

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

問題描述

關于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但是結果出現在views:commentCreate里它的效果是等于articleUpdate,即新增留言變成修改文章內容python - Django 表單問題?

python - Django 表單問題?

問題解答

回答1:

表單是類,你取出數據,為什么不填充到表單中。

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美日韩一区二区在线 | 亚洲黄色av| 久久久久久久一级 | 精品无码久久久久久国产 | 久热9| 久久精品欧美一区二区三区不卡 | 久久视频免费看 | 久久精品国产99国产精品亚洲 | 久久久久国产精品一区 | av中文天堂 | 日韩中出 | 亚洲欧美久久 | 日韩av一区二区在线观看 | 日韩欧美一级精品久久 | 日本一区二区三区四区 | 国产美女在线观看 | 国产精品福利视频 | 欧美视频免费在线 | 四虎永久免费在线 | 久久精品综合 | 午夜天堂精品久久久久 | 亚洲精品久久久久久宅男 | 国产精品美女久久久久aⅴ国产馆 | 国产精品久久久乱弄 | 国产精品伦理一区 | 日韩毛片 | 欧美精品99| 一区在线播放 | 亚洲性人人天天夜夜摸 | 国产成人99久久亚洲综合精品 | 成人h视频在线 | 国户精品久久久久久久久久久不卡 | 久久久久久久av麻豆果冻 | 色免费看 | 婷婷中文字幕 | 在线亚洲人成电影网站色www | 超碰激情 | 欧美日韩在线综合 | 日韩精品一二三 | 午夜视频在线免费观看 | 精品国产青草久久久久96 |