1

我尝试翻译我的 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'}))
        ...     

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

我如何翻译“在此处输入您的问题”和“输入投票选项”?

4

2 回答 2

2

_只是一个标识符,就像f. 当你调用一个函数f时,你用 来做这件事f(…),所以_它是一样的:_(…)

因此,您可以使用以下方法修复语法错误:

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'}))
于 2020-04-11T22:39:31.917 回答
2

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

(_'Type your question here')

这应该是:

_('Type your question here')
于 2020-04-11T22:42:15.720 回答