0

表格.py

Date_Format = (
    ('0', ' dd / mm / yyyy'),
    ('1', 'mm / dd / yyyy'),
)

Time_Format = (
    ('0', ' 12 hour AM / PM '),
    ('1', ' 24 hour '),
)

class SettingsForm(forms.ModelForm):
    date_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Date_Format)
    time_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Time_Format)
    class Meta:
        model = Settings
        fields = ['date_format','time_format']

视图.py

def date_format(request):
  #  settingsForm = SettingsForm({'user':request.user}) 
    settings = Settings.objects.get(user=2)
    settingsForm = SettingsForm(instance=settings) 
  #  dateformatForm = DateFormatForm()   
   # timeformatForm = TimeFormatForm()
    settingsForm = SettingsForm()
    if request.method =='POST':
      #  dateformatForm = DateFormatForm(request.POST)
      #  timeformatForm = TimeFormatForm(request.POST)
        settingsForm = SettingsForm(request.POST,instance=settings)
        if (settingsForm.is_valid()):  
            settings=settingsForm.save()
       #     timeformatForm.save()
            return redirect('/member/follow-up/') 


    return render_to_response( 'incident/date_format.html',
                  {
                   'about_menu': True,
                   'SettingsForm':settingsForm,

                 },
                 context_instance=RequestContext(request))

模型.py

class Settings(models.Model):
    user = models.ForeignKey(User, null=True)
    date_format = models.CharField('Date format', max_length=100)
    time_format = models.CharField('Time format', max_length=100)

模板是

<form action="/member/date-format/" method="POST"> {% csrf_token %}
{{ SettingsForm.date_format }}
{{ SettingsForm.time_format }}
</form>

我在这里使用模型表单将表单值存储在数据库中。

如果我选择单选按钮选项。相应的值将保存在数据库中。但是选中的单选按钮将取消选中页面是否重新加载。我不知道使用什么逻辑来执行此操作。

此外,是否有任何方法可以在不使用 modelsForm 的情况下将单选按钮的值保存在模板中。

谢谢

4

1 回答 1

0

在您看来:

def date_format(request):
  #  settingsForm = SettingsForm({'user':request.user}) 
    settings = Settings.objects.get(user=2)
    settingsForm = SettingsForm(instance=settings) 
  #  dateformatForm = DateFormatForm()   
   # timeformatForm = TimeFormatForm()
    settingsForm = SettingsForm()
    ....

您正在重置settingsForm带有空表单的表单,这将显示没有来自 instance 的任何值的表单settings。您可能想要删除该行。

于 2013-05-08T05:45:47.467 回答