在Django管理员更改列表页面中未选择新行(\ n)

时间:2013-06-24 15:08:52

标签: python django django-admin

我在list_display中添加了一个小函数来显示多个到多个字段,我试图显示由换行符分隔的m2m对象。 但新行分离器无法正常工作

我的django管理员:

class ArticleAdmin(admin.ModelAdmin):
     list_filter = ["category",]
     list_display = ('category','article_type', 'get_domains')
     filter_horizontal = ("domain",)

     def get_domains(self, obj):
        x ="\n".join([str(s.name) for s in obj.domain.all()])
        print x  # printing fine
        return x

     get_domains.short_description = 'Domains'   

print语句表现良好:

   0 errors found
   Django version 1.4.5, using settings 'settings'
   Development server is running at http://127.0.0.1:8000/
   Quit the server with CONTROL-C.
   mykitchenremodelinglosangeles.com:8000
   kitchenremodelerchicagoil.com:8000
   localhost:8000

我已附加管理屏幕输出:

enter image description here

你可以建议吗?

2 个答案:

答案 0 :(得分:7)

HTML中的浏览器会忽略新行。如果您想以不同的方式显示它们,我认为您应join使用<br>代替\n并使用allow_tags

class ArticleAdmin(admin.ModelAdmin):
    list_filter = ["category",]
    list_display = ('category','article_type', 'get_domains')
    filter_horizontal = ("domain",)

    def get_domains(self, obj):
        x ="<br>".join([str(s.name) for s in obj.domain.all()])
        print x  # printing fine
        return x

    get_domains.allow_tags = True

希望这有帮助!

答案 1 :(得分:0)

 class ArticleAdmin(admin.ModelAdmin):
     list_filter = ["category",]
     list_display = ('category','article_type', 'get_domains')
     filter_horizontal = ("domain",)

     def get_domains(self, obj):
        x ="<br>".join([str(s.name) for s in obj.domain.all()])
        return x

     get_domains.short_description = 'Domains'
     get_domains.allow_tags=True   

它完成了这项工作! django管理员很酷: - )

除此之外还有其他方法吗?