python - 没有 django.forms 的 Django CRUD 系统通过 {{ id }} 来查看函数
问题描述
我正在编写一个 CRUD(不使用 django.forms 或通用表单)我无法将 id 值从 toUpArti() 传递给 updateArti() 谁能告诉我如何或提及解决此问题的另一种方法
文章/views.py:
def toUpArti(request, arti_id):
d = ArticleItem.objects.get(id=arti_id)
return render(request, 'artiUp.html')
def updateArti(request):
u = ArticleItem.objects.filter(id=x).update(title=request.POST['title'],
body=request.POST['body'])
return HttpResponseRedirect('/article/')
解决方案
我假设您的网址类似于
path('article/<int:arti_id/', views.toUpArit, name='view-article')
那么你的回报应该是HTTPRespoonseRedirect(reverse('view-article', kwargs='arti_id:u.id}))
使用DRY 原则
推荐阅读
- c++ - 使用 QT 和 c++ 在 txt 文件中搜索的代码
- c# - OpenXML - 表创建,我如何创建表而不需要 excel 来修复它们
- jquery - 标签值不会在 jquery click 上立即更改
- json - 使用 Dropwizard 时配置 Jackson 映射器
- python - 根据其他列中的值填充 pandas 列
- php - 本地主机重定向您太多次。如何防止用户从管理员访问页面
- mysql - 如何在1 Mysql中求和,乘和除
- python-3.x - 如何从文件夹中读取 csv 文件,读取前 10 个文件并保存在一个 csv 中,读取接下来的 10 个文件并保存在另一个 csv 文件中等等
- react-native - Apollo Graphql 分页因限制而失败
- swagger - Swagger 生成的 Java 客户端没有重载调用,即使查询参数被标记为可选