0
현재 사용자가 제품을 편집 할 수있는 양식을 렌더링하려고하고 있습니다. 현재 양식이 하나의 긴 열로 표시되고 있습니다.바삭한 양식 레이아웃을 반복적으로 구성 하시겠습니까?
내가 두 개의 컬럼으로 분할 것을 요청되었지만 사용하여 생성되는 ModelForm
로 인해 문제가 발생하고 modelform_factory()
나는 새로운 사업부의의 모든를 삽입 할 수 바삭 레이아웃 객체를 생성 할 수있는 방법은 없나요 두 가지 형태의 객체?
참고 : 양식의 길이는 미리 알 수 없습니다.
코드보기 :
def layout_from_form(form, columns=2):
field_count = sum(1 for i in form) # form specified it's iterable but is not len() friendly
for field_number, _ in enumerate(form):
if field_number % columns == 0:
max_length_field = field_number + 2
if field_number + 2 > field_count:
max_length_field = field_count
try:
selected_forms = form.helper[field_number:max_length_field]
selected_forms.wrap(Div, css_class="span6")
selected_forms.wrap_together(Div, css_class="row-fluid")
except:
assert False, (field_count, field_number, max_length_field)
def edit_product(request, bought_in_control_panel_id, item_uuid):
boughtin_model = get_model_for_bought_in_control_panel(bought_in_control_panel_id)
item = boughtin_model.objects.get(pk=item_uuid)
BoughtinForm = modelform_factory(boughtin_model, exclude=("uuid", "date_time_updated", "date_time_created",
"manufacturer"))
if request.method == "POST":
boughtin_form = BoughtinForm(request.POST, instance=item)
if boughtin_form.is_valid():
boughtin_form.save()
return redirect(reverse('view_product', kwargs={'bought_in_control_panel_id': bought_in_control_panel_id,
'item_uuid': item_uuid}))
else:
boughtin_form = BoughtinForm(instance=item)
boughtin_form.helper = FormHelper(boughtin_form)
boughtin_form.helper.form_action = reverse('edit_product', kwargs={'bought_in_control_panel_id': bought_in_control_panel_id,
'item_uuid': item_uuid})
boughtin_form.helper.add_input(Submit('submit', 'Submit'))
layout_from_form(boughtin_form)
return render_to_response('suppliers/products/edit_product.html', {'item': item,
'boughtin_form': boughtin_form,
'bought_in_control_panel_id': bought_in_control_panel_id})
예 레이아웃 개체 : 귀하의 질문에
Layout(
Div(
Field('name'),
Field('type'),
css_class="row-fluid"
),
Div(
Field('uuid'),
Field('dave'),
css_class="row-fluid"
),
.... Etc ad infinitum ....
)
양식의 길이는 어떤 방식으로 미리 알 수 없습니까? –
바삭한 양식의 문서에서 "이동 중에도 레이아웃을 업데이트 한 적이 있습니까?": http://django-crispy-forms.readthedocs.org/en/latest/dynamic_layouts.html 특히 [wrap] (http : //django-crispy-forms.readthedocs.org/en/latest/dynamic_layouts.html#wrap) action은 당신이 찾고있는 행동 일 수도 있습니다. 보기에서 (가변 길이의) 들어오는 양식의 길이를 결정하고 그런 식으로 동적 레이아웃을 만들 수있는 것처럼 느껴집니다. –
글쎄, 나는 modelform_factory에 의해 생성 된 양식에 필드 수를 얻는 방법을 모르며 매번 전달되는 동일한 모델에 의존 할 수 없습니다. – Jharwood