1

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" ?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Aytek
  • 224
  • 4
  • 16

2 Answers2

2

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'}))
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

The invalid syntax error is caused by the following code:

(_'Type your question here')

This should be:

_('Type your question here')
Sijmen
  • 477
  • 6
  • 12