如何使用Django(django-mentions)

时间:2014-09-17 20:14:46

标签: django django-forms django-templates django-views

是否有人成功使用了django-mentions lib - https://pypi.python.org/pypi/django-mentions。如果是这样,你可以帮我解决下面的问题(谢谢)。我按照文档(但看起来我没做正确的事)。我在模板中收到此错误消息urlize_mentions

 TemplateSyntaxError at /
    Invalid filter: 'urlize_mentions'
    Request Method: GET
    Request URL:    http://localhost:8000/
    Django Version: 1.6.6
    Exception Type: TemplateSyntaxError
    Exception Value:    
    Invalid filter: 'urlize_mentions'

以下是我遵循的步骤

步骤1. pip install django-mentions

第2步。添加'提及'在settings.py

中的INSTALLED_APPS中
INSTALLED_APPS = (
    ...
    'mentions',
    ...

)

步骤3.在网址模式中添加提及

urlpatterns = patterns('',
    ...
    url(r'^mentions/', include('mentions.urls')),
    ...

)

步骤4.实施提及提供程序,我在我的应用程序中创建了mentionsprovider.py

from mentions.mentions import Provider
from django.contrib.auth.models import User

    #Implement provider
    class UserProvider(Provider):
        model = User
        def get_title(self, obj):
            return obj.username
        def search(self, request, term):
            return self.get_queryset().filter(username__istartswith=term)

步骤5.在settings.py

中添加此提及提供程序
 MENTIONS_PROVIDERS = {
        # You can put your provider anywhere you want
        'default': [
            'appname.mentionsprovider.UserProvider'
        ]
    }

步骤6.使用mentions.forms.MentionTextArea。我是在我的应用程序的forms.py

中完成的
from mentions.forms import MentionTextarea


class NoteForm(forms.ModelForm):
    class Meta:
        model = Note
        widgets = {
            'text': MentionTextarea
        }

在我的模板中我有类似的东西(我试过各种组合)

{{form.note|urlize_mentions}}
{{form.text|urlize_mentions}}
{{note.text|urlize_mentions}}

0 个答案:

没有答案