get_absolute_url无效
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了get_absolute_url无效相关的知识,希望对你有一定的参考价值。
我正在使用下面显示的github源代码进行练习,但我仍然坚持使用get_absolute_url
方法。无法访问svariable.html
。它总是重定向到base.html
。任何帮助将非常感激。请在下面找到代码,几乎没有变化:
https://github.com/GeoffMahugu/Django-Ecommerce
models.朋友
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.朋友
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.朋友
urlpatterns = [
url('',Home, name = 'Home'),
url(r'^home/(?P<pk>d+)/$', SingleProduct , name='SingleProduct'),
url(r'^(?P<slug>[w-]+)/$', SingleCategory, name='SingleCategory'),
]
答案
问题出在您的URL中:Home的模式匹配每个路径。你需要在开始和结束时锚定。
url('^$', Home, name='Home')
以上是关于get_absolute_url无效的主要内容,如果未能解决你的问题,请参考以下文章
Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]
get_absolute_url:找不到“文章详细信息”的反向错误
在 Django auth 中设置 `get_absolute_url`
get_absolute_url 中的 django 完整 url