Aytek
Aytek

Reputation: 224

Invalid syntax error in Django form translate

I try translate my forms.py (placeholder, choices, etc) but i take syntax error. my code is here;

from django import forms
from django.utils.translation import ugettext_lazy as _

class CreatePollForm(forms.Form):
        title = forms.CharField(max_length = 300, label="", widget=forms.Textarea(attrs={'rows':'1','cols':'20', 'placeholder': (_'Type your question here'),'class': 'createpoll_s'}))
        polls = forms.CharField(max_length = 160, required=False, label="", widget=forms.TextInput(attrs={ 'placeholder': (_'Enter poll option'), 'class': 'votes firstopt','id':'id_polls1','data-id':'1'}))
        ...     

if i use like this, i take syntax error.

how can i translate "Type your question here" and "Enter poll option" ?

Upvotes: 1

Views: 218

Answers (2)

Sijmen
Sijmen

Reputation: 506

The invalid syntax error is caused by the following code:

(_'Type your question here')

This should be:

_('Type your question here')

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

Thhe _ is just an identifier, just like f. When you call a function f, you do this with f(…), so for _ it is the same: _(…).

You thus can fix the syntax errors with:

class CreatePollForm(forms.Form):
    title = forms.CharField(max_length = 300, label="", widget=forms.Textarea(attrs={'rows':'1','cols':'20', 'placeholder': _('Type your question here'),'class': 'createpoll_s'}))
    polls = forms.CharField(max_length = 160, required=False, label="", widget=forms.TextInput(attrs={ 'placeholder': _('Enter poll option'), 'class': 'votes firstopt','id':'id_polls1','data-id':'1'}))

Upvotes: 2

Related Questions