Django JavaScript Translation gettext is not defined - Stack Overflow

My javascript function contains the following:document.getElementById("example").innerHTML =

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745408253a4626411.html

相关推荐

  • Django JavaScript Translation gettext is not defined - Stack Overflow

    My javascript function contains the following:document.getElementById("example").innerHTML =

    5小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信