当我尝试使用python和Django重定向到购物车时,我总是遇到这个错误

2024-03-28 21:38:22 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我是新来的编码和django和我试图创建一个简单的电子商务网站,但当点击一个按钮重定向到购物车,但它没有通过。 当我启动服务器时会出现此错误


     File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\sre_parse.py", line 831, in _parse
        raise source.error(err.msg, len(name) + 1) from None
    re.error: redefinition of group name 'id' as group 2; was group 1 at position 25

下面是html代码

            <!-- Product Pricing -->
            <div class="product-price">
              <span>{{i.product_price}}</span>
              <a href="/cart/{{i.id}}/{{customer.id}}" class="cart-btn">Add to cart</a>
              <p>{{customer.id}}</p>
              <a href="" class="cart-btn">go to cart</a>
            </div>
   

我的观点代码


    def cart(request,cid,pid) :
        p_info=product_info.objects.get(id=pid)
        c_info=login_details.objects.get(id=cid)
        return render(request, 'customer side/cart.html')

我的appurl代码



    urlpatterns=[
        path('register',views.save_login_info),
        path('cart/<int:id>/<int:id>/',views.cart),
        path('login',views.logging_in,name='login'),
        path('list',views.product_catalogue),
        path('new',views.add_product),
        path('savingdata',views.addnew,name='saving'),
    
        enter code here
    
    ]

``

the error apppears to have something to do with the url part of the cart when i remove on of the integer id from cart/int(id)/int(id) the error disappears but i need both numbers.
i tried assigning values to the variables in my views code but nothing seems to fix the problem.
please help.


Tags: ofthetopathnameininfoid
1条回答
网友
1楼 · 发布于 2024-03-28 21:38:22

您需要两个名为ID的参数,一旦重定向发生,第二个ID将覆盖第一个ID。只需将第二个更改为customer_id或其他内容,即可解决您的问题

  urlpatterns=[
        path('register',views.save_login_info),
        path('cart/<int:product_id>/<int:customer_id>/',views.cart),
        path('login',views.logging_in,name='login'),
        path('list',views.product_catalogue),
        path('new',views.add_product),
        path('savingdata',views.addnew,name='saving'),
    
    ]

相关问题 更多 >