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

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

form.save() 不工作我的 form.save 更新數(shù)據(jù)不工作

form.save() 不工作我的 form.save 更新數(shù)據(jù)不工作

湖上湖 2023-02-22 17:00:17
form.save() 不工作我的 form.save 更新數(shù)據(jù)不工作當(dāng)我嘗試更新我的帖子它顯示原始未經(jīng)編輯的帖子它發(fā)送保存更新的版本。我不知道是什么導(dǎo)致錯(cuò)誤 idk 如果它的視圖或任何在模板如果有人可以提供幫助,我將不勝感激,請(qǐng)?zhí)峁椭?。這是代碼:views.pyfrom django.shortcuts import renderfrom django.shortcuts import HttpResponseRedirectfrom .models import Postfrom .forms import PostFormdef post_list(request):    posts = Post.objects.all()    context = {        'post_list': posts    }    return render(request, "posts/post_list.html", context)def post_detail(request, post_id):    post = Post.objects.get(id=post_id)    context = {        'post': post    }    return render(request, "posts/post_detail.html", context)def post_create(request):    form = PostForm(request.POST or None)    if form.is_valid():        form.save()        return HttpResponseRedirect('/posts')    context = {        "form": form,        "form_type": 'Create'    }    return render(request, "posts/post_create.html", context)def post_update(request, post_id):    post = Post.objects.get(id=post_id)    form = PostForm(request.POST or None, instance=post)    if form.is_valid():        form.save()        return HttpResponseRedirect('/posts')    context = {        "form": form,        "form_type": 'Update'    }    return render(request, "posts/post_update.html", context)def post_delete(request, post_id):    post = Post.objects.get(id=post_id)    post.delete()    return HttpResponseRedirect('/posts')urls.pyfrom django.urls import pathfrom .views import post_list, post_detail, post_create, post_update, post_deleteurlpatterns = [    path('', post_list),    path('create/', post_create),    path('<post_id>/', post_detail),    path('<post_id>/update', post_update),    path('<post_id>/delete', post_delete),]post_update.html<h1>welcome to post {{ form_type }}</h1><form method="POST" action=".">    {% csrf_token %}    <p>{{ form.as_p }}</p>    <button type="submit">{{ form_type }}</button></form>
查看完整描述

1 回答

?
暮色呼如

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

action="."帶你從<post_id>/update<post_id>/。您可以通過幾種方式解決此問題:

  1. 將其更改為action="",這將從提交<post_id>/update<post_id>/update

  2. 在 URL 中添加斜杠,即path('<post_id>/update/', ...). 然后action="."將從提交<post_id>/update/<post_id>/update/

  3. 使用{% url %}標(biāo)簽而不是對(duì)操作進(jìn)行硬編碼。在你的情況下,你需要做一些改變,所以我將把它作為一個(gè)挑戰(zhàn)留給你。有關(guān)反轉(zhuǎn) URL 的文檔應(yīng)該有所幫助。


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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