【问题标题】:Trying to make the program add a string after an id试图让程序在 id 之后添加一个字符串
【发布时间】:2020-10-26 12:17:07
【问题描述】:

我正在尝试让一个按钮将我带到http://127.0.0.1:8000/admin/Inventory_Management/item/7/delete/ 加上我添加的 url,它把我带到了http://127.0.0.1:8000/admin/Inventory_Management/item/7。没有'/delete'我试图添加,但我不太清楚如何,即使经过一些研究它仍然无法正常工作。

相关视图

def Warehouse(request, pk):
  Warehouse = warehouse.objects.get(id=pk)
  items = item.objects.all()

  context = {
      'items': items,
      'warehouse': Warehouse,
  }

  return render(request, 'Inventory_Management/warehouse.html', context)

相关网址

path('admin/Inventory_Management/item/<str:pk>/', Warehouse, name="change_"),

按钮

<a href="{% url 'change_' item.id %}" class="btn btn-danger btn-sm" role="button"> x</a>

我尝试了以下方法,但正如我所料,它没有用......

<a href="{% url 'change_' item.id/delete %}" class="btn btn-danger btn-sm" role="button"> x</a>

对不起,如果这是一个愚蠢的问题。我还不是一个伟大的程序员。 任何帮助表示赞赏!请帮忙!

【问题讨论】:

    标签: python django url django-urls


    【解决方案1】:

    要转到...admin/Inventory_Management/item/7/delete/,您必须声明一个与此网址匹配的网址。所以你的路径应该是

    path('admin/Inventory_Management/item/<str:pk>/delete', Warehouse, name="change_"),
    

    你的模板应该是

    <a href="{% url 'change_' item.id %}" class="btn btn-danger btn-sm" role="button"> x</a>
    

    注意:如果你想删除一个项目,你必须声明另一个控制器。您的 Warehouse 似乎没有删除任何内容。

    【讨论】:

    • @AliReda-M 你能把答案标记为正确吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多