python - Crispy-forms InlineRadios 不显示我的模型状态

标签 python django django-crispy-forms

很奇怪,我有一个带有 Charfield 的模型,它有一些可能的值

class Entry(models.Model):
    title = models.CharField(
        max_length=100,
        blank=True,
        null=True,
        help_text="We will refer to your entry by this title.")
    production_type = models.CharField(
        blank=False,
        null=True,
        max_length=32,
        default=None,
        choices=  (('Agency', 'Agency'),('Brand', 'Brand'),))

当我渲染一个简单的 ModelForm 时

class EntryForm(forms.ModelForm):
    class Meta:
        model = Entry
        fields = [
            'title', 'production_type'
        ]
    def __init__(self, request, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.layout = Layout(
            'title',
            'production_type'
        )

它为选择了正确状态的 production_type 字段呈现下拉列表。如果我如下添加 InlineRadios 小部件,则单选按钮在呈现时不会选择模型状态。我总是得到第一选择。表单提交正常。

class EntryForm(forms.ModelForm):
    class Meta:
        model = Entry
        fields = [
            'title', 'production_type'
        ]
    def __init__(self, request, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.layout = Layout(
            'title',
             InlineRadios('production_type', css_class='production_type_holder')
        )

有什么方法可以内联单选按钮行为并跟踪我的模型状态?

最佳答案

可能有一些错误:

  1. EntryFormmodel = Entry 行,而必须是 model = Payment ,如您描述的模型 Payment
  2. EntryForm.__init__ 中缺少行 super(EntryForm, self).__init__(*args, **kwargs)
  3. 你的 __init__ 声明包含冗余(?)参数 request 所以声明必须是

    def __init__(self, *args, **kwargs): #without request
    

请尝试此更改。

您的代码(在我的版本中,如下)在单选按钮的正常行为下工作正常,它通常会创建新的付款并打印现有的。

没有变化的 models.py

表单.py

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Fieldset, ButtonHolder, Submit
from crispy_forms.bootstrap import InlineRadios

class EntryForm(forms.ModelForm):
    class Meta:
        model = Payment # !not Entry
        fields = [
            'title', 'production_type'
        ]
    def __init__(self, *args, **kwargs):  #!without request
        super(EntryForm, self).__init__(*args, **kwargs)  #this line present
        self.helper = FormHelper()

        self.helper.layout = Layout(
            'title',
            InlineRadios('production_type'),
            Submit('submit', 'Submit'), # i miss your submit button, so adding here
        )

View .py

from django.views.generic.edit import CreateView


# it creates new payment 
class PaymentCreate(CreateView):
    model = Payment
    form_class= EntryForm
    template_name='payment_form.html'
    success_url = '/payment/'

# this usual view just print form with already created instance
def PaymentView (request):
    form =  EntryForm(instance = Payment.objects.get(id=4))
    c = {'form': form}
    return render(request,'payment_form.html', c)

payment_form.html

{% load crispy_forms_tags %}
<html>
<head>
</head>
<body>

    {% csrf_token %}
    {% crispy form %}

</body>
</html>

urls.py:

...
#i made 2 views, one for 'usual' view, one for generic view
url(r'payment_view/', PaymentView), 
url(r'payment/', PaymentCreate.as_view()),
...

关于python - Crispy-forms InlineRadios 不显示我的模型状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33882411/

相关文章:

python - Django 密码重置电子邮件主题

python - 编写不返回对象的 Django View

python - 构建 DSL 查询语言

Django CrispyForms - 在单独的引导选项卡中渲染表单集的每种形式

python - 如何使用每个选择的模板覆盖 ModelChoiceField/ModelMultipleChoiceField 默认小部件

Python:修改现有的键值对并在键不存在时引发异常

python - 为什么调用 argparse.parse_args() 或 .parse_args(sys.argv) 时会有所不同

python - 如何在 Textblob python 语言检测库中获得语言扩展

javascript - Django:Crispy 表单的 Ajax 验证

python - PyGtk 中命令 "python setup.py egg_info"失败,错误代码为 1