获取url参数以处理通用视图

时间:2019-04-28 10:28:53

标签: html django django-generic-views

我想编写一个模板视图,该视图根据URL路径显示模型的不同字段。例如,如果path为

http://127.0.0.1:8000/trip/2/1/

我将从数据库中获得第二次访问(有效),并且1应该给(描述HTML)一个描述字段。我认为我不知道如何将其处理到context_processor。你有什么想法吗?

views.py

class TripDescriptionCreate(LoginRequiredMixin, UpdateView):

    model = Trip
    template_name = 'tripplanner/trip_arguments.html'
    fields = ["description", "city", "country", "climate", "currency", "food", "cost", "comment", "accomodation",
    "car", "flight", "visa", "insurance", "remarks"]
    context_object_name = 'trips'
    success_url = '/'

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

trip_arguments.html

 <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">{{ trips.tripName }}</legend>
                    {% if field_id == 1 %}
                        {{ form.description|as_crispy_field }}
                    {% elif field_id == 2 %}
                        {{ form.city|as_crispy_field }}
                    {% endif %}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Update</button>
            </div>
        </form>

urls.py

path('trip/<int:pk>/<int:field_id>', TripDescriptionCreate.as_view(), name='trip-fullfill'),

1 个答案:

答案 0 :(得分:0)

所以我想到了这个主意。在我的html中,我添加了以下几行:

API_KEY