为什么我的动态表单集不显示任何字段

时间:2019-10-16 08:19:03

标签: django python-3.x django-forms formset inline-formset

我的目标是为汽车定义一个上传表单,供用户上传具有所有相关详细信息的汽车,并在同一页面上添加图像。

管理表单完美运行,我只想在自定义页面中模拟它-一项非常困难的任务!我使用的是this tutorial,字段已更改为匹配我的项目。

我还使用django-dynamic-formset jQuery插件来提供用户需要的其他图像字段。

我已经坚持了一个星期,我不确定问题出在哪里,所以我将分享很多代码。

class ImagesInline(admin.TabularInline):
    model = Image

@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
    list_display = ('manufacturer', 'car_model', 'model_year', 'mileage', 'status', 'date_added', 'price', 'seller')
    list_filter = ('manufacturer', 'status', 'transmission')
    fieldsets = (
        ('General information', {
            'fields': ('manufacturer', 'car_model',
            'model_year', 'price', 'vin', 'mileage', 'transmission', 'engine_displacement', 
            'forced_induction', 'drivetrain', 'description',)
        }),
       ('Availability', {
            'fields': ('status', 'seller')
        }),
    )
    inlines = [ImagesInline]

这是CarCreate视图

class CarCreate(generic.CreateView):

    model = Car
    # fields = '__all__'
    success_url = reverse_lazy('car-detail.html')

    slug_field = 'id'
    slug_url_kwarg = 'car_create'

    template_name = 'showroom/car_create.html'
    form_class = CarImageForm
    success_url = None

    def get_context_data(self, **kwargs):
        data = super(CarCreate, self).get_context_data(**kwargs)
        if self.request.POST:
            data['images'] = CarImageFormSet(self.request.POST)
        else:
            data['images'] = CarImageFormSet()
        return data

    def form_valid(self, form):
        context = self.get_context_data()
        images = context['images']
        with transaction.atomic():
            form.instance.created_by = self.request.user
            self.object = form.save()
            if images.is_valid():
                titles.instance = self.object
                titles.save()
        return super(CarCreate, self).form_valid(form)

    def get_success_url(self):
        return reverse_lazy('showroom:car_detail', kwargs={'id': self.object.id})

模板car_create.html

{% extends "base.html" %}

{% load crispy_forms_tags %}
{% block content %}
<div class="container">
    <div class="card">
        <div class="card-header"></div>
            <h2>Upload a car</h2>
        </div>
    <div class="card-body">
        {% crispy form %}
    </div>
</div>
{% endblock content %}

custom_layout_object.py

from crispy_forms.layout import LayoutObject, TEMPLATE_PACK
from django.shortcuts import render
from django.template.loader import render_to_string 

class Formset(LayoutObject):
    template = "showroom/formset.html"

    def __init__(self, formset_name_in_context, template=None):
        self.formset_name_in_context = formset_name_in_context
        self.fields = []
        if template:
            self.template = template

    def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
        formset = context[self.formset_name_in_context]
        return render_to_string(self.teplate, {'formset': formset})

formset.html

{% load crispy_forms_tags %}

{% extends base.html %}

{{ formset.management_form|crispy }}

    {% for form in formset.forms %}
        <tr class="{% cycle 'row1' 'row2' %} formset_row-{{ formset.prefix}}">
            {% for field in form.visible_fields %}
                <td>
                    {# Include the hidden fields in the form #}
                    {% if forloop.first %}
                        {% for hidden in form.hidden_fields %}
                            {{ hidden }}
                        {% endfor %}
                    {% endif %}
                    {{ field.errors.as_ul }}
                    {{ field|as_crispy_field}}
                </td>
            {% endfor %}
        </tr>
    {% endfor %}

{% block javascript %}
    <script src="{% static '/js/django-dynamic-formset/jquery.formset.js' %}"></script>
    <script type="text/javascript">
        $('.formset_row-{{ formset.prefix }}').formset({
            addtext: 'add another',
            deleteText: 'remove',
            prefix: '{{ formset.prefix }}',
        });
    </script>
{% endblock %}

尽管如此,创建表单如下所示:

enter image description here

什么时候看起来像这样

enter image description here

控制台终端没有任何错误。

0 个答案:

没有答案