2013-12-16 11 views
0

我想让ListView工作。 我想要一个显示模型中所有对象的视图。就像索引页一样。基于类的列表视图挂断如何查询所有

当我通过它的DetailView查找一个对象的slu and和页面的作品。
此列表视图中给我的错误:

Page not found (404) at /oferto/listview/

ofertoj.views

class OfertoListView(ListView): 
    model = Oferto 

class OfertoHome(ListView): 
    model = Oferto 

    def get_context_data(self,**kwargs): 
     context = Oferto.objects.all() 
     return context 

urls.py

url(
    regex=r"^$", 
    view = OfertoHome.as_view(), 
    name ="oferto_home" 
    ), 

url(
regex=r"listview/$", 
view=OfertoListView.as_view(), 
name="oferto_listview" 
), 

models.py

class Oferto(models.Model): 
    user = models.ForeignKey(User) 

    name = models.CharField(max_length=150) 
    description = models.TextField(max_length=3000) 

    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',)) 
    tags = tagging.fields.TagField() 

    image = models.ImageField(upload_to='Ofertoj',blank=True, null=True) 

    def get_absolute_url(self): 
     return reverse('oferto_detail', kwargs={'slug': self.slug}) 

    def __unicode__(self): 
     return self.name 

    def get_tags(self): 
     return Tag.objects.get_for_object(self) 

父urls.py

(r'^oferto/',include('ofertoj.urls')), 
+0

这是一个错误? 'self.request.Get.get(“q”)'应该有'GET'而不是'Get' .. – mariodev

+0

thx是的,这是一个错字 – Klanestro

回答

0

有一两件事是:

你跟一个QuerySet更换contextoferto_home观点:

def get_context_data(self,**kwargs): 
    context = Oferto.objects.all() 
    return context 

应该是这样的:

def get_context_data(self,**kwargs): 
    context = super(OfertoHome, self).get_context_data(**kwargs) 
    return context 

ListView控件默认情况下应检索所有对象


您的urls.py命名空间是?如果没有,我相信,你的网址可能是

url(
regex=r"^listview/$", 
view=OfertoListView.as_view(), 
name="oferto_listview" 
), 

如果你想在前面加上你可以创建一个URL命名空间或explicityly使用oferta在该应用程序的名字上面的网址将在/listview/

可用url like:

regex=r"^ofertta/listview/$",

+0

谢谢修复我的theerto_home,该listview工作正常,但默认listview仍然给我一个404没有找到。我也相信我有网址的命名空间 – Klanestro

+0

我想我真的不知道什么是命名空间。但看看我的父urls.py – Klanestro