django在一行中形成表单的字段

时间:2011-03-25 09:15:12

标签: django forms

我有一个类似

的表格
from django import forms
from captcha.fields import CaptchaField
from django.shortcuts import render_to_response

from OEConnector import *

class Personal_info_updateForm(forms.Form):
    title_choices = ( ('herr', 'Herr'), ('frau', 'Frau'))
    #Dates tuple list
    dates=[]
    for x in range(1,32):        
        dates.append(tuple([x.__str__(),x.__str__()]))        
    dates = tuple(dates)
    #Months tuple list
    months = (
          ('jan','Januar'),
          ('feb','Februar'),
          ('mar','Marz'),
          ('apr','April'),
          ('may','Mai'),
          ('jun','Juni'),
          ('jul','Juli'),
          ('aug','August'),
          ('sep','September'),
          ('oct','Oktober'),
          ('nov','November'),
          ('dec','Dezember'),
          )
    #Years tuple list
    years=[('','')]
    for y in range(1930,2050):        
        years.append(tuple([y.__str__(),y.__str__()]))        
    years = tuple(years)
    #Get list of countries from OpenERP and create countries tuple list

    title = forms.ChoiceField(label="Anrede", choices=title_choices, widget=forms.RadioSelect)
    first_name = forms.CharField(label="Vorname", required=False)
    last_name = forms.CharField(label="Nachname", required=False)
    date_of_birth = forms.ChoiceField(choices=dates, widget=forms.Select, required=False)
    month_of_birth = forms.ChoiceField(choices=months, widget=forms.Select, required=False)
    year_of_birth = forms.ChoiceField(choices=years, widget=forms.Select, required=False)
    email = forms.EmailField(label='Email',required=False)
    phone = forms.CharField(label="Phone", required=False)
    mobile = forms.CharField(label="Mobile", required=False)

我在我的模板中调用此表单。它显示我喜欢 出生日期:
出生月份: 出生年份:

现在我想要这三个字段就像一行一样 出生日期:出生月份:出生年份:

我的模板就像

<form action="" method="POST">
    <table style="color:black;text-align:left; margin-left: 20px;">
        {{ form.as_table }}

    </table>
    <input type="submit" value="UPDATE">
</form>

我想要 出生日期:出生月份:出生年份:

提前致谢

1 个答案:

答案 0 :(得分:4)

而不是分割字段,您可以将DateField与SelectDateField小部件一起使用,例如

forms.DateField(widget=SelectDateWidget(years=[y for y in range(1930,2050)]))

这将显示为表格中的一行 - [month] [day] [year]