Django 表单翻译中的无效语法错误

Invalid syntax error in Django form translate

我尝试翻译我的 forms.py(占位符、选项等),但出现语法错误。我的代码在这里;

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'}))
        ...     

如果我这样使用,我会出现语法错误。

如何翻译 "Type your question here" 和 "Enter poll option"?

_只是一个标识符,就像f一样。当您调用函数 f 时,您使用 f(…) 执行此操作,因此对于 _ 它是相同的:_(…).

因此您可以通过以下方式修复语法错误:

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

无效语法错误是由以下代码引起的:

(_'Type your question here')

这应该是:

_('Type your question here')