Django + trac-wiki到html markdown'SafeText'对象没有属性'get' [英] Django + trac-wiki to html markdown 'SafeText' object has no attribute 'get'

查看:260
本文介绍了Django + trac-wiki到html markdown'SafeText'对象没有属性'get'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将trac wiki markdown格式作为html显示在我的网站上。我试过跟随这个其他 SO问题我如何使用trac wiki格式,但代码段会返回当我运行它时出错。值得注意的是,这个问题和代码已经差不多4年了。任何想法如何让这个工作?

I am trying to render trac wiki markdown format as html for display on my website. I tried following this other SO question 'how do i use trac wiki formatting', but the code snippet returns an error when I run it. It should be noted that the question and code are nearly 4 years old. Any idea how I can get this to work?

在我的urls.py中,我只需调用 tracwiki (from

In my urls.py i simply call the tracwiki (from the snippet) view.

#urls.py

url(r'^$',                       'tracwiki',              name='index'),

#views.py

"""
Usage:

{% load tracwiki %}

{{ object.body|tracwiki }}

# Logic from http://groups.google.com/group/trac-dev/msg/479decac43883dc0
"""

from trac.test import EnvironmentStub, Mock, MockPerm 
from trac.mimeview import Context 
from trac.wiki.formatter import HtmlFormatter 
from trac.web.href import Href

from django.utils.safestring import mark_safe
from django import template
register = template.Library()

env = EnvironmentStub() 
req = Mock(href=Href('/'), abs_href=Href('http://www.example.com/'), 
           authname='anonymous', perm=MockPerm(), args={})
context = Context.from_request(req, 'wiki')

@register.filter
def tracwiki(s):
    return mark_safe(HtmlFormatter(env, context, s).generate())

以下是返回的错误:

[01/Apr/2014 18:40:53] "GET / HTTP/1.1" 500 60948


AttributeError at /

'SafeText' object has no attribute 'get'

Request Method:     GET
Request URL:    http://xxx.xxx.xxx.xxx/
Django Version:     1.5.5
Exception Type:     AttributeError
Exception Value:    

'SafeText' object has no attribute 'get'

Exception Location:     /mysite/local/lib/python2.7/site-packages/django/middleware/clickjacking.py in process_response, line 30
Python Executable:  /Envs/mysite/bin/python
Python Version:     2.7.5


推荐答案

Th问题是您已经创建了一个自定义模板过滤器,但您已将其保存在 views.py 中,因此django将其视为 view 。这是错误的 - 您的代码不属于 views.py ,您不需要添加任何内容到 urls.py 。要详细说明...

The problem is that you have created a custom template filter, but you have saved it in views.py, so django is treating it like a view. This is wrong - your code doesn't belong in views.py and you don't need to add anything to urls.py. To elaborate...

使用您当前的配置,当您的站点根目录的请求由URLConf(通过'^ $ '你添加的' tracwiki()函数返回一个 HttpResponse 对象 - 因为它认为这是一个视图

With your current configuration, when a request to your site root is picked up by the URLConf (via the '^$' pattern you added), django expects the tracwiki() function to return a HttpResponse object - because it thinks that this is a view.

但是,如果我们查看这个 tracwiki()函数,可以看到我们不返回 HttpResponse 对象通过 render() render_to_response() 等等,而是返回一个 SafeText 没有一个 .get()属性,与 HttpResponse 对象不同,henc当中间件在我们返回的对象调用 .get()时,是例外。

However if we look at this tracwiki() function, you can see we do not return a HttpResponse object via render() or render_to_response() etc. Instead we are returning a SafeText object which does not have a .get() attribute, unlike a HttpResponse object, hence the exception when the middleware calls .get() on the object we returned.

相反,您需要删除您添加到您的 urls.py 中的行,并在要呈现此标记的相应HTML模板中使用模板过滤器。例如

Instead you need to remove the line you added to your urls.py, and use the template filter inside the appropriate HTML template where you want to render this markup. For example

{{ some_variable|tracwiki }}

如果您不熟悉模板过滤器,这里是一些内置的过滤器被使用

If you are unfamiliar with template filters, here are some built-in filters being used.

您还需要从 views.py中移除 tracwiki() 文件,并进入新的 templatags 目录中的新模块。详细了解文档(但是请记住,在调用过滤器之前,您需要在模板中加载此新模块)

You also need to move the tracwiki() logic out of the views.py file and into a new module inside a new templatags directory. Read more about that in the docs (but remember you will need to load this new module inside the template before you call the filter)

{% load new_filter_module %}

这篇关于Django + trac-wiki到html markdown'SafeText'对象没有属性'get'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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