开发者

Indent-text in the SelectField in Django

I am trying to indent text in a multiple choice field.

def __init__(self):
    super(RegisterFull,self).__init__()
    sectorList = Sector.objects.all()    

sArray = []    
for s in sectorList:    
    sArray.append((s.id,"    "+s.__unicode__()))
self.fields["subsector"].choices = sArray

I have tried this and

sArray.append((s.id,"  "+s.__unicode__()))

However, I couldn't achieve what I wanted because the "&" character is replaced by & (of course) and empty space is tr开发者_如何学JAVAimmed. Why, when html page is created?

Could you suggest a way?

Thanks.


I'd suggest using cascading style sheets (CSS) to format the fields as you want. HTML is supposed to focus mainly on the layout of your document, while CSS can do the fine tuning of formating. So add a class to the widget of your Form;

e.g.,

class UserForm(forms.Form):
    name = forms.CharField(widget = forms.TextInput(attrs={'class':'indented'}))

and then have in CSS at the top of the template (or move to an separate style sheet)

<style type="text/css">
.indented {
    text-indent: 20px;
}
</style>

If you really wanted, you could do something simpler like:

class UserForm(forms.Form):
    name = forms.CharField(widget = forms.TextInput(attrs={'style':'text-indent: 20px;'})).

EDIT: To get your solution to work (with &nbsp;)

from django.utils.safestring import mark_safe

def __init__(self):
    super(RegisterFull,self).__init__()
    sectorList = Sector.objects.all()    

    sArray = []    
    for s in sectorList:    
        sArray.append((s.id,mark_safe("&nbsp; &nbsp; "+s.__unicode__())))
        self.fields["subsector"].choices = sArray
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜