我的网站上收到“网址未找到错误”。这些是我的文件:
myapp.urls:
url(r'^produkty/', include('supplement.urls', namespace='supplements')),
supplement.urls:
urlpatterns = [
url(r'^(?P<slug>[-\w]+)/$', SupplementDetailView.as_view(), name='detail'),
url(r'^$', SupplementListView.as_view(), name='list'),]
supplement.views:
class SupplementListView(ListView):
def get_queryset(self):
return Supplement.objects.all()
class SupplementDetailView(DetailView):
queryset = Supplement.objects.all()
注意:queryset = Supplement.objects.all()和get_queryset(self):return Supplement.objects.all()在SupplementDetailView中不起作用
supplement.models:
class Supplement(models.Model):
name = models.CharField(blank=False, null=False, help_text=_("product's name"), max_length=50)
price = models.IntegerField(blank=True, null=True)
portion = models.CharField(max_length=100, blank=True, null=True, help_text=_("portion"))
img = models.ImageField(default='kitchen/No-image-available.jpg', upload_to='products/', blank=True, null=True)
description = RichTextField()
contains = models.CharField(blank=True, null=True, max_length=3000, help_text=_("contains"))
slug = models.SlugField(max_length=150, blank=True, null=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('supplements:detail', kwargs={"slug": self.slug})
class Meta:
ordering = ['-name']
verbose_name = _('supplement')
verbose_name_plural = _('supplements')
def rl_pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator(instance)
pre_save.connect(rl_pre_save_receiver, sender=Supplement)
SupplementListView和SupplementDetailView的模板放在BASE_DIR / supplement / templates / supplement中。
注意:SupplementDetailView就像一个魅力。它加载有关在url中传递的产品的信息(例如:myapp.pl/produkty/product-slug),但产品列表(myapp.pl/produkty)返回“未找到URL错误”
提前感谢您的帮助,我一直在努力解决这个错误。
答案 0 :(得分:0)
我终于找到了答案:我的myapp.urls文件中有这个:
url(r'^', include('opinions.urls', namespace='home')),
url(r'^produkty/', include('supplement.urls', namespace='supplements')),
所以每当我进入“myapp.pl/produkty/”时,Django都相信,“produkty”是一个意见模型的slu ..我切换了两个url模式(因此首先加载了namespace ='supplements'的模式),一切都很好。