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



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

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

我如何翻译"在这里键入你的问题"one_answers"输入投票选项"?

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

(_'Type your question here')

这应该是:

_('Type your question here')

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

最新更新