python - Django 表單問(wèn)題?
問(wèn)題描述
關(guān)于Django表單問(wèn)題先給出models.py和forms.py
圖片描述

再給出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表單并不是同一類,一個(gè)是ArticleForm一個(gè)是CommentForm但是結(jié)果出現(xiàn)在views:commentCreate里它的效果是等于articleUpdate,即新增留言變成修改文章內(nèi)容

問(wèn)題解答
回答1:表單是類,你取出數(shù)據(jù),為什么不填充到表單中。
else:form = CommentsForm(request.POST)if form.is_valid():
相關(guān)文章:
1. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. docker-compose中volumes的問(wèn)題3. boot2docker無(wú)法啟動(dòng)4. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.5. nignx - docker內(nèi)nginx 80端口被占用6. dockerfile - 為什么docker容器啟動(dòng)不了?7. node.js - antdesign怎么集合react-redux對(duì)input控件進(jìn)行初始化賦值8. docker容器呢SSH為什么連不通呢?9. java - SSH框架中寫分頁(yè)時(shí)service層中不能注入分頁(yè)類10. 關(guān)于docker下的nginx壓力測(cè)試

網(wǎng)公網(wǎng)安備