自定义包含表单标签

时间:2019-05-31 07:52:30

标签: python django forms model tags

我有一个表格:

class TripSearchForm(forms.Form):
    departure = PlaceModelChoiceField(queryset=Place.objects.places_for_segment(),
                                      widget=autocomplete.ModelSelect2(url='autocomplete'))
    destination = PlaceModelChoiceField(queryset=Place.objects.places_for_segment(),
                                        widget=autocomplete.ModelSelect2(url='autocomplete'))

,并且我正在尝试将此表单转换为django包含标签,为了将来可以多次使用,我还有一个view可以从表单中计算所有数据并将适当的上下文返回给渲染模板。 我也想使用单个url来实现此功能。

这是我的模板标签,我不确定是否正确:

@register.inclusion_tag('places/search_form.html')
def search_form1(request):
    context = dict()
    if request.POST:
        form = TripSearchForm(request.POST)
        if form.is_valid():
            departure = request.POST['departure']
            destination = request.POST['destination']
            return redirect('route_search', departure=departure, destination=destination)
    form = TripSearchForm()
    return {'form': form}

我的模板:

<div class="container-fluid">
        <div class="row justify-content-center mt-4">
            <div class="col-10">
                {% load trip_search_form %}
                {% search_form request %}
                <div class="col-10 mt-5">
                    {% if trips %}
                        {% for trip in trips %}
                            {{ trip.2 }}
                            <ul class="list-group list-group-horizontal">
                                {% with trip.0|first as fsegment and trip.0|last as lsegment %}
                                    <li class="list-group-item">Travel time:{{ trip.3.duration }} (hours)</li>
                                    <li class="list-group-item">Costs: {{ trip.3.cost }} (uah)</li>
                                    <li class="list-group-item">Distance: {{ trip.3.distance }} (km)</li>
                                     <li class="list-group-item">
                                        <a href="{% url 'route_detail' fsegment.departure.slug lsegment.destination.slug trip.2 %}">
                                            Details
                                        </a>
                                    </li>
                                {% endwith %}
                            </ul>
                            <br>
                        {% endfor %}
                    {% else %}
                        <h1>Trip Not Found!</h1>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    </div>

所以,这是我以前的view

class TripsResultsListView(View):
    template = 'places/trip-search-results.html'
    context = dict()

    def get(self, request):
        form = TripSearchForm()
        self.context['form'] = form
        return render(request, template_name=self.template, context=self.context)

    def post(self, request):
        form = TripSearchForm(request.POST)
        trip_types = [CHEAPEST, SHORTEST, FASTEST]
        if form.is_valid():
            departure = Place.objects.get(uuid=request.POST['departure'])
            destination = Place.objects.get(uuid=request.POST['destination'])
            trips = []
            for trip_type in trip_types:
                trip = calculated_trips(departure, destination, trip_type)
                if trip is not None:
                    trips.append(trip)
            self.context['trips'] = trips
            self.context['form'] = form
            return render(request, template_name=self.template, context=self.context)
        self.context['form'] = form
        return redirect('route_search', context=self.context)

url:

path('trips/', views.TripsResultsListView.as_view(), name='route_search'),

它工作得很完美,但是现在我应该将其转换为视图,而无需任何form初始化,只是简单的计算,而我不知道该怎么做。

如果您有任何想法,请分享一下;如果可以提供一些示例,请先谢谢您

0 个答案:

没有答案