2013-12-16 24 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