Django Extra Views,CreateWithInlinesView - 如何让InlineFormSet传递给_form.html?

时间:2017-12-19 09:30:49

标签: django django-forms inline-formset

如何在InlineFormSet中显示building_form.html

views.py

from django.contrib.auth.mixins import LoginRequiredMixin
from extra_views import InlineFormSet, CreateWithInlinesView

from buildings.models import Building, Gallery
from .forms import BuildingForm

class GalleryInline(InlineFormSet):
    model = Gallery
    fields = ['image', 'caption']

class BuildingCreateView(LoginRequiredMixin, CreateWithInlinesView):
    model = Building
    form_class = BuildingForm
    inlines = [GalleryInline, ]
    template_name = 'cms/building_form.html'

forms.py

from django.contrib.gis.forms import OSMWidget, ModelForm

from buildings.models import Building


class BuildingForm(ModelForm):
    class Meta:
        model = Building
        fields = '__all__'
        widgets = {
            'point': OSMWidget(
                attrs={
                    'map_width': 600,
                    'map_height': 600,
                    'template_name': 'gis/openlayers-osm.html',
                    'default_lat': 56,
                    'default_lon': 10,
                    'default_zoom': 7,
                }
            )
        }

    class Media:
        css = {
            'all': (
                'https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.css',
                'gis/css/ol3.css',
            )
        }
        js = (
            'https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.js',
            'gis/js/OLMapWidget.js',
        )

building_form.html

{% block content %}

    <form enctype="multipart/form-data" method="post" action="">
        {% csrf_token %}
        <table>
            {{ form.as_table }}
        </table>
        <input type="submit" value="Submit"/>
    </form>

{% endblock %}

0 个答案:

没有答案