Python 脆脆的形式在意大利的唐';不显示我的模型状态
奇怪的是,我有一个带有Charfield的模型,它有一些可能的值Python 脆脆的形式在意大利的唐';不显示我的模型状态,python,django,django-crispy-forms,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.Char
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'),))
当我渲染一个简单的模型时
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'
)
它为选择了正确状态的生产类型字段呈现下拉列表。如果我像下面那样添加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')
)
有什么方法可以同时获得内嵌单选按钮行为并跟踪我的模型状态?可能会出现一些错误:
EntryForm
有一行model=Entry
,而必须是model=Payment
,就像您在模型中描述的Payment
EntryForm.\uuuuu init\uuuuu
缺少行super(EntryForm,self)。\uuuuu init\uuuu(*args,**kwargs)
\uuuu init\uuuu
声明包含冗余(?)参数请求
,因此声明必须是
def __init__(self, *args, **kwargs): #without request
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
)
views.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)
支付表单.html
{% load crispy_forms_tags %}
<html>
<head>
</head>
<body>
{% csrf_token %}
{% crispy form %}
</body>
</html>
原来是虚惊一场。我的设计师彻底改变了css,打破了简单的表单。我以为我已经隔离了这些更改,但是一旦我删除了所有应用程序生成的CSS,它们都工作得很好。很抱歉出现错误警报。请为您的表单提供模板。。。很高兴看到views.py tooThat的帮助,但是不正确的模型只是一个转录错误。我将尝试从表单中删除请求,尽管我不是很有希望。要点是不要错过调用父init方法并删除requestThank。我也在给超级构造函数打电话。我会更改请求的内容。另外,我将进行最后一次检查,以确保没有javascript或css影响这一点。此外,将请求作为构造函数的第一个参数也没有问题,只要后面跟着*args、**kwargs。我将再次查看js和css,然后对其进行破解:)
...
#i made 2 views, one for 'usual' view, one for generic view
url(r'payment_view/', PaymentView),
url(r'payment/', PaymentCreate.as_view()),
...