调用django视图从另一个视图返回结果

时间:2014-06-12 11:16:23

标签: python django django-views

我有两个观点,定义如下:

class ListResultView(LoginRequiredMixin, ListView):
    model = Result

class GalleryView(LoginRequiredMixin, ListView):
    model = Result
    template = 'gallery.html'
    context_object_name = 'gallery'

因此,ListResultView()使用隐式定义的result_list.html作为模板,结果被明确定义为上下文模型,而GalleryView(相同数据的更漂亮列表)使用明确定义的模板' gallery html的'并且上下文对象被定义为' gallery'。

我使用以下urls.py调用它们(这是主要的urls.py,而不是包含的内容):

from django.conf.urls import patterns, include, url
import lc.views
from django.views.generic import TemplateView
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', TemplateView.as_view(template_name="about.html"), 
        name='indexpage'), 
    url(r'^gallery/$', lc.views.GalleryView.as_view(), name='gallery'),
    url(r'^admin/', include(admin.site.urls), name='admin'),  
    url(r'^listquery/', lc.views.ListCView.as_view(),
        name='s_queries',),
    url(r'^listresult/', lc.views.ListResultView.as_view(),
        name='s_results',),
    url(r'^new/', lc.views.CreateCQuery.as_view(),
        name='query_new',),    
    url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
    url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/login'},              name='logout'),
    url(r'^deletes/(?P<pk>\d+)/$', lc.views.DeleteS.as_view(), name='delete_s'),
    url(r'^deleter/(?P<pk>\d+)/$', lc.views.DeleteResult.as_view(), name='delete_result'),
    url(r'^resultview/(?P<pk>\d+)/$', lc.views.ResultDetailView.as_view(),
        name='resultview'),
    url(r'^notyet', TemplateView.as_view(template_name="not_impl.html"), name="notyet",),
)

我遇到的问题是,当我调用GalleryView网址时,我收到了ListResultView响应。我无法看到任何错误消息,并想知道是否有人可以指出我在哪里出错,或者如何调试它。我目前的想法是抛弃基于类的视图并重写为基于函数的视图,这样我就可以更好地处理正在发生的事情,但是由于时间压力,我宁愿不这样做。

1 个答案:

答案 0 :(得分:1)

在您的templatetemplate_name更改GalleryView