Django自定义视图到管理页面 [英] Django custom view into admin page

查看:122
本文介绍了Django自定义视图到管理页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义视图。

I have created a custom view.

如何将视图插入管理员?

How can I insert the view into the admin?

对于正常的管理员类,我们只需将其注册到管理站点:

For a normal admin class, we can just simply register it to the admin site:

class ListAdmin(admin.ModelAdmin):
   ...

admin.site.register(List, ListAdmin)

我试图在admin.py中覆盖get_url,question_list是视图:

I tried to override get_url in admin.py, question_list is the view:

class ListAdmin(admin.ModelAdmin):
    def list_view(self, request):
        return question_list(request)

    def get_urls(self):
        urls = super(ListAdmin, self).get_urls()
        list_urls = patterns('', r'^list/$', self.list_view())

        return list_urls + urls

admin.site.register(question_list, ListAdmin)

这是question_list视图:

This is the question_list view:

def question_list(request):
    #questions = Question.objects.filter(topic__icontains = 1)
    questions = Question.objects.all()
    return render_to_response('admin/question_list.html', {'questions':questions})
question_list = staff_member_required(question_list)

我得到'function'对象不是可迭代的错误。

谢谢。

推荐答案

根据您提供的信息,您应该检查Django文档的这一部分:

Based on the information you provided you should check this part of Django's documentation:

将视图添加到管理站点(注意:链接对于版本1.5有效,因为1.3版本不再被支持 - 解决方案仍然有效)。

Adding views to admin sites (note: the link is valid for version 1.5 since version 1.3 is not supported anymore - the solution is still valid).

然后,您可以检查这个博文以及问题,以获取更多的灵感和细节。

Then you could check this blog post and this question for some further inspiration and details.

根据你的例子我真的不明白为什么你不使用常规的 ModelAdmin 与一些过滤选项

Based on your example I really don't get why you just don't use a regular ModelAdmin with some filtering options:

class QuestionAdmin(admin.ModelAdmin):
    list_filter = ('topic',)

这篇关于Django自定义视图到管理页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆