Django自定义管理员网址未定义

时间:2013-03-01 20:36:40

标签: django django-admin django-urls

我正在尝试向模型管理员添加两个网址。

class JobTitleAdmin(admin.ModelAdmin):
    inlines = [OccupationTagInline, ]
    model = JobTitle
    search_fields = ['title',]
    list_filter = ['status',]
    actions =['add_to_job_category', 'move_to_job_category' ]

    def add_to_job_category_view(self, request):

        return render_to_response(
            'admin/job_title/select_job_category.html',
            {
                'action': 'add',
                'featured_occupations': Occupation.objects.filter(featured=True),
                'title_ids': array(request.GET['foo'])
                },
            context_instance=RequestContext(request)
            )

    def get_urls(self):
        urls = super(JobTitleAdmin, self).get_urls()
        custom_urls = patterns('',
            url(r'^add_to_job_category/$', self.admin_site.admin_view(self.add_to_job_category_view), name='admin_jobtitle_add_to_cat',),
            url(r'^move_to_job_category/$', self.admin_site.admin_view(self.move_to_job_category_view), name='admin_jobtitle_move_to_cat',)
    )

        return custom_urls + urls

为简单起见,我省略了视图代码。

这两个模式没有定义,因为我试图访问它们并且它没有在django debud 404页面中列出,我得到了404。然而,方法get_urls被正确调用(实际上两次,不确定为什么)。

可能出现什么问题?

2 个答案:

答案 0 :(得分:2)

我已成功完成此操作,但我复制了ModelAdmin.get_urls wrap装饰器。也许试试:

from functools import update_wrapper
class JobTitleAdmin(admin.ModelAdmin):
    inlines = [OccupationTagInline, ]
    model = JobTitle
    search_fields = ['title',]
    list_filter = ['status',]
    actions =['add_to_job_category', 'move_to_job_category' ]

    def add_to_job_category_view(self, request):

        return render_to_response(
            'admin/job_title/select_job_category.html',
            {
                'action': 'add',
                'featured_occupations': Occupation.objects.filter(featured=True),
                'title_ids': array(request.GET['foo'])
                },
            context_instance=RequestContext(request)
            )

    def get_urls(self):
        urls = super(JobTitleAdmin, self).get_urls()

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        custom_urls = patterns('',
            url(r'^add_to_job_category/$',
                 wrap(self.add_to_job_category_view),
                 name='admin_jobtitle_add_to_cat',),
            url(r'^move_to_job_category/$',
                 wrap(self.move_to_job_category_view),
                 name='admin_jobtitle_move_to_cat',)
        )

        return custom_urls + urls

答案 1 :(得分:0)

从您的代码中,您似乎尝试向ModelAdmin课程添加一些action intermediate views

这是通过将函数名称放在actions属性中来实现的。如果您只是添加自定义操作,则无需添加自定义网址。您的示例可以像这样修改:

class JobTitleAdmin(admin.ModelAdmin):
    inlines = [OccupationTagInline, ]
    model = JobTitle
    search_fields = ['title',]
    list_filter = ['status',]
    actions =['add_to_job_category', 'move_to_job_category' ]

    def add_to_job_category(self, request, queryset):

        return render_to_response(
            'admin/job_title/select_job_category.html',
            {
                'action': 'add',
                'featured_occupations': Occupation.objects.filter(featured=True),
                'title_ids': array(request.GET['foo'])  # <-- this would probably be changed to use `queryset` function argument
                },
            context_instance=RequestContext(request)
            )
    add_to_job_category.short_description = "Add to job category"

ModelAdmin类将知道如何调用自定义函数并将操作添加到操作下拉列表中。中间响应视图add_to_job_category需要知道如何从操作中获取输入以确定选择了哪些项目。

如果您确实要添加自定义网址,那么您的get_urls()功能似乎是正确的。请注意,get_urls返回的网址与admin/<app>/<model>相关。

如果您的应用已命名为myapp且您的模型已命名为JobTitle,那么点击add_to_job_category视图的网址将为:

http://some.site/admin/myapp/jobtitle/add_to_job_category/