在哪里存储我的index.html和索引视图的非静态Django主页

时间:2013-11-17 17:28:38

标签: django django-templates django-views

我正在创建一个Django项目,目前有3个应用程序(产品,问题和选择) - 我已经将它们全部单独/一起运行,因为我喜欢并使用名称空间并包含在我的网址中(我的urls.py如下所示......)

### urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^products/', include('products.urls', namespace='products')),
    url(r'^questions/', include('questions.urls', namespace='questions')),
    url(r'^choices/', include('choices.urls', namespace='choices')),
    url(r'^admin/', include(admin.site.urls)),
)

现在我想添加一个索引页面(可在localhost:8000 /访问),这将允许我访问所有创建的模型。我很满意我的观点。...

## views.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from products.models import Product
from questions.models import Question
from choices.models import Choice


class IndexView(generic.ListView):
    template_name = 'index.html'
    context_object_name = 'latest_product_list'

    def get_queryset(self):
        return Product.objects.all()

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['questions'] = Question.objects.all()
        context['choices'] = Choice.objects.all()
        return context

我想知道的是,urls.py条目的最明智组合是什么,views.py的位置和index.html的位置允许我在着陆页上显示我的模型的组合?

谢谢,

马特

1 个答案:

答案 0 :(得分:2)

创建另一个应用程序(我倾向于将其命名为home)。这样,您的家庭应用程序可以从产品,问题和选择中导入模型。

应用布局

home/urls.py
home/views.py
home/templates/index.html

包括您的主页网址,就像您的其他应用程序一样(但不要使用前缀)

url(r'^', include('home.urls', namespace='home')),