admin管理员组文章数量:1429035
My javascript function contains the following:
document.getElementById("example").innerHTML = gettext("This is an example");
My urls.py looks like:
urlpatterns = [
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
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')),
]
And in my template I have:
<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
The translation above using gettext() does not work. A Reference Error es up saying gettext() is not defined. However, in the same javascript file i have:
var monthNames = [gettext("January"), gettext("February"), gettext("March"), gettext("April"), gettext("May"), gettext("June"), gettext("July"), gettext("August"), gettext("September"), gettext("October"), gettext("November"), gettext("December")];
And that does not prompt a reference error. The month translations work but the example one does not.
My javascript function contains the following:
document.getElementById("example").innerHTML = gettext("This is an example");
My urls.py looks like:
urlpatterns = [
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
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')),
]
And in my template I have:
<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
The translation above using gettext() does not work. A Reference Error es up saying gettext() is not defined. However, in the same javascript file i have:
var monthNames = [gettext("January"), gettext("February"), gettext("March"), gettext("April"), gettext("May"), gettext("June"), gettext("July"), gettext("August"), gettext("September"), gettext("October"), gettext("November"), gettext("December")];
And that does not prompt a reference error. The month translations work but the example one does not.
Share Improve this question asked Mar 9, 2017 at 19:08 ALUWALUW 3971 gold badge5 silver badges18 bronze badges4 Answers
Reset to default 4I am not sure, but try checking the order of your script. See, if you are using the gettext() function for the example above before the script tag where you load the javascript-catalog.
I literally had the same exact issue, even when I had the script tag e before the gettext().
The simple fix was to make sure the catalog came before the app in urlpatterns:
urlpatterns = [
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
...
<your app>
]
Here is plete guideline to add Admin Date Picker in Front end.
forms.py
from django.contrib.admin import widgets
from django import forms
from .models import Student
class StudentForm(forms.ModelForm):
class Media:
css = {
'all': (
'/static/admin/css/widgets.css',
)
}
js = [
# '/admin/jsi18n/',
'/static/admin/js/core.js',
]
class Meta:
model = Student
fields = [
"first_name",
"last_name",
"birth_date",
]
widgets = {
'birth_date': widgets.AdminDateWidget()
}
urls.py
from django.views.i18n import JavaScriptCatalog
from django.urls import path
urlpatterns = [
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]
add_student.html
<form action="{% url 'some_url' %}" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
{{ form.media }}
<script src="/jsi18n/"></script>
add this before jquery block. Hope this might help
本文标签: Django JavaScript Translation gettext is not definedStack Overflow
版权声明:本文标题:Django JavaScript Translation gettext is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745408253a2657347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论