最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Django JavaScript Translation gettext is not defined - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 4

I 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

发布评论

评论列表(0)

  1. 暂无评论