我正在使用下面显示的github源代码进行练习,但我仍然无法使用get_absolute_url
方法。无法访问svariable.html
。它总是重定向到base.html
。任何帮助将非常感激。请在下面找到代码,但几乎没有变化:
https://github.com/GeoffMahugu/Django-Ecommerce
models.py
class Product(models.Model):
title = models.CharField(max_length=120)
categories = models.ManyToManyField('Category', blank=True)
default_category = models.ForeignKey('Category', related_name='default_category', null=True, blank=True,
on_delete=models.CASCADE)
description = models.TextField(null=True, blank=True)
price = models.DecimalField(decimal_places=2, max_digits=20)
default_image = models.ImageField(upload_to=image_upload_to_prod, blank=True, null=True)
active = models.BooleanField(default=True)
featured = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
slug = models.SlugField(blank=True, null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('Products:SingleProduct', kwargs={'pk': self.pk})
views.py
def SingleProduct(request, pk):
objects = get_object_or_404(Product, pk=pk)
vari = Variation.objects.filter(product=objects)
obj_cat = objects.default_category
obj_prod = Product.objects.filter(default_category=obj_cat).exclude(pk=objects.pk).order_by('-pk')[:3]
title = '%s' % (objects.title)
cart_id = request.session.get('cart_id')
object = get_object_or_404(Cart, pk=cart_id)
cart_count = object.cartitem_set.count()
cart_items = object.cartitem_set.all()
context = {
'vari': vari,
'title': title,
'objects': objects,
'obj_prod': obj_prod,
'cart_count': cart_count,
'cart_items': cart_items
}
template = 'svariable.html'
return render(request, template, context)
def Home(request):
request.session.set_expiry(0)
title = 'Welcome to E-shop Germany'
products = Product.objects.all().order_by('?')
featured = Product.objects.filter(featured=True).order_by('?')[0:]
prod_no = products.count()
categories = Category.objects.all()
cart_id = request.session.get('cart_id')
if cart_id == None:
cart = Cart()
cart.save()
cart_id = cart.id
request.session['cart_id'] = cart_id
cart = Cart.objects.get(pk=cart_id)
cart_count = cart.cartitem_set.count()
cart_items = cart.cartitem_set.all()
context = {
'title': title,
'products': products,
'featured': featured,
'prod_no': prod_no,
'categories': categories,
'cart': cart,
'cart_count': cart_count,
'cart_items': cart_items
}
return render(request, 'base.html', context)
root urls.py
urlpatterns = [
url(r'^Products/', include('products.urls',namespace='Products' )),
url(r'^Carts/', include('carts.urls', namespace='Carts' )),
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urls.py
urlpatterns = [
url('',Home, name = 'Home'),
url(r'^home/(?P<pk>\d+)/$', SingleProduct , name='SingleProduct'),
url(r'^(?P<slug>[\w-]+)/$', SingleCategory, name='SingleCategory'),
]
答案 0 :(得分:2)
问题出在您的网址中:Home的模式与每条路径匹配。你需要在开始和结束时锚定。
url('^$', Home, name='Home')