django .urls.exceptions.NoReverseMatch: Реверс для 'update_cart' с аргументами '(' ',)' не найден. Попробован 1 шаблон (ов): ['cart / (? P [^ /] +) $'] [18 / Apr / 2020 14:05:02] "GET / checkout / HTTP / 1.1" 500 157543 <--- это сообщение об ошибке, которое я получаю в терминале, когда пытаюсь go перейти на страницу оформления заказа. </p>
просмотр. html
{% for item in cart.products.all %}
<tr><td> {{ item }} </td><td>{{item.price}}</td>
<td><a href='{% url "update_cart" item.slug %}'> Remove</a></td></tr>
{% endfor %}
</table>
<br/>
<a href='{% url "checkout" %}'>Checkout</a>
{% endif %}
</div>
</div>
{% endblock content %}
views.py для заказов
from django.urls import reverse
from django.shortcuts import render, HttpResponseRedirect
# Create your views here.
from carts.models import Cart
def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
return HttpResponseRedirect(reverse("fuisce-home"))
context = {}
template = "fuisce/home.html"
return render(request, template, context)
urls.py
from django.urls import path
from . import views
from carts import views as cart_views
from orders import views as order_views
urlpatterns = [
path('', views.home, name='fuisce-home'),
path('subscription/', views.subscription, name='fuisce-subscription'),
path('oneoff/', views.oneoff, name='fuisce-oneoff'),
path('about/', views.about, name='fuisce-about'),
path('contact/', views.contact, name='fuisce-contact'),
path('cart/', cart_views.view, name='cart'),
path('cart/<slug>', cart_views.update_cart, name='update_cart'),
path('checkout/', order_views.checkout, name='checkout'),
]
проблема возникает, когда я перемещаю HttpResponse с чуть ниже проверки def, до ниже cart = Cart.objects.get (id = the_id). (изменение в коде прилагается ниже). Кто-нибудь знает, как позволить ему принять это изменение?
def checkout(request):
***return HttpResponseRedirect(reverse("fuisce-home"))***
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
***return HttpResponseRedirect(reverse("fuisce-home"))***
views.py для корзины
def update_cart(request, slug):
request.session.set_expiry(120000)
try:
the_id = request.session['cart_id']
except:
new_cart = Cart()
new_cart.save()
request.session['cart_id'] = new_cart.id
the_id = new_cart.id
cart = Cart.objects.get(id=the_id)
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
pass
except:
pass
if not product in cart.products.all():
cart.products.add(product)
else:
cart.products.remove(product)
new_total = 0.00
for item in cart.products.all():
new_total += float(item.price)
request.session['items_total'] = cart.products.count()
cart.total = new_total
cart.save()
return redirect('cart')
models.py - product
class Product(models.Model):
product_id = models.AutoField
product_name = models.CharField(max_length=50)
price = models.DecimalField(max_digits=10, decimal_places=2)
desc = models.TextField()
slug = models.SlugField(unique=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
image = models.ImageField(upload_to='fuisce/images', default="")
def __str__(self):
return self.product_name