[英]How do I pass anchor tag id to urls in Django?
I'm trying to build an "add to cart" functionality for a pizza order website.我正在尝试为披萨订购网站构建“添加到购物车”功能。 To do this, I set up anchor tags and I need to send the specific item id to the views function from the anchor tag so that item can be added to the cart.
为此,我设置了锚标签,我需要将特定的商品 ID 从锚标签发送到视图 function,以便可以将商品添加到购物车。 I'm getting a TypeError with this code saying "add_to_cart() missing 1 required positional argument: 'item_id'".
我收到一个 TypeError 代码,上面写着“add_to_cart() 缺少 1 个必需的位置参数:'item_id'”。 I believe this is because the item.id variable is not being passed to urls.py.
我相信这是因为 item.id 变量没有被传递给 urls.py。 Does anyone know the correct way to solve this?
有谁知道解决这个问题的正确方法? I am using Django 3.0.8 so I don't know if RegEx will work.
我正在使用 Django 3.0.8 所以我不知道 RegEx 是否可以工作。 Thanks in advance!
提前致谢!
urls.py:网址.py:
from django.urls import path, include
from django.contrib import admin
from django.contrib.auth.forms import UserCreationForm
from . import views
from users import views as users_views
urlpatterns = [
path('', views.index, name="index"),
path('admin/', admin.site.urls),
path('login/', views.login_view, name="login"),
path('logout/', views.logout_view, name = "logout"),
path('register/', users_views.register_view, name = "register"),
path('menu/', views.menu, name = "menu"),
path('cart/', views.add_to_cart, name = "add_to_cart"),
path('cart/<int:item_id>', views.add_to_cart, name = "add_to_cart")
]
views.py:视图.py:
def add_to_cart(request, item_id):
#query database for the correct item
order_id = Pizza.objects.get(id = item_id)
#add the new order to the cart
new_item = Cart.objects.create(order_id)
#test connection
return render(request, "orders/homepage.html")
html template: html模板:
{% extends "orders/base.html" %}
{% block body %}
<h2>Pizza Menu</h2>
<form action="{% url 'add_to_cart' %}" method="post">
{% csrf_token %}
<table>
<tbody>
<thead>
Regulars
</thead>
{% for item in pizza %}
<tr>
<td> {{ item.item }}, {{ item.toppings }}, Price: {{ item.price }}</td>
<td>
<a href="{% url 'add_to_cart' item.id %}" data-id = "{{ item.id }}" id = "{{ item.id }}"><button type="info" name="item">Add to Cart</button></a>
</td>
</tr>
</tbody>
{% endfor %}
</table>
</form>
<!--
<table>
{% for item in regular_pizza %}
<ul>
<li>
{{ item }}
</li>
</ul>
{% endfor %}
{% for item in sicilian_pizza %}
<ul>
<li>
{{ item }}
</li>
</ul>
{% endfor %}
</table> -->
</div>
{% endblock %}
Thank you again to anyone who can help!再次感谢任何可以提供帮助的人!
You are redifining this view:您正在重新定义此视图:
path('cart/', views.add_to_cart, name = "add_to_cart"),
path('cart/<int:item_id>', views.add_to_cart, name = "add_to_cart")
Delete the first one, and make item_id
optional using regex, so you just have:删除第一个,并使用正则表达式使
item_id
成为可选,因此您只需:
path('cart/(?P<item_id>[0-9]*)', views.add_to_cart, name = "add_to_cart")
Then, since item_id
is optional:然后,由于
item_id
是可选的:
def add_to_cart(request, item_id=None):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.