Django javascript翻译在页面加载时给出错误

时间:2017-03-09 17:09:56

标签: javascript html django

这就是我的urls.py看起来的样子:

js_info_dict = {
    'domain': 'djangojs',
    'packages': ('project',),
}

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('project.urls')),
    url(r'^login/$', auth_views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name = 'login'),
    url(r'^logout/$', auth_views.logout, {'next_page': '/login'}),
    url(r'^i18n/', include('django.conf.urls.i18n')),
]

urlpatterns += i18n_patterns('', (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict), )

这就是我在html中的内容:

<script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' %}"></script>

我已经运行django-admin makemessages -d djangojs -l fr并对djangojs.po文件进行了更改。然后我跑了django-admin compilemessages。我看到了djangojs.mo文件。当我访问我的网站时,它给了我一个指向上面HTML代码行的AttributeError。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

from django.conf.urls.i18n import i18n_patterns
js_info_dict = {
    'domain': 'djangojs',
    'packages': ('project',),
}
urlpatterns = i18n_patterns(
    url(r'^jsi18n/$', js_info_dict, name='javascript-catalog'),
)
  

在模板中

`<script type="text/javascript" src="{% url 'javascript-catalog' %}">`</script>

答案 1 :(得分:0)

对于遇到此问题的其他人,除了运行django-admin makemessages -d djangojs -l frdjango-admin compilemessages之外,我还是采取了解决问题的方法。

urls.py应如下所示:

from django.views.i18n import JavaScriptCatalog
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('project.urls')),
    url(r'^login/$', auth_views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name = 'login'),
    url(r'^logout/$', auth_views.logout, {'next_page': '/login'}),
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]

这应该是模板:

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>