Django如何通过表单按钮传递url参数?

时间:2018-08-31 13:05:59

标签: django django-forms django-templates django-views

您好,我是django的新手,我阅读了django的文档,但是我使用了我想预订汽车的带参数的网址。预订时,我需要汽车ID和驾驶员ID。重定向到我的预订页面后。我现在预订的按钮未发送驾驶员ID和汽车ID。请帮我。 例如。 我有一个约翰作为司机,他点击奥迪来预定。得到他的网址后,我想将其保存到我的预订数据库中。在我的html“立即预订”中单击按钮后,它不会呈现到下一页预订汽车,也不会获得汽车ID和驾驶员ID。 请帮助我。

对不起,我的英语。如果听不懂,请在评论中让我知道,以便多给您解释并分享更多内容。我只是该领域的初学者,所以只是学习。

观看次数

@csrf_protect
def rentacar_car(request, car_id,driver_id):

    try:
        args['car'] = Car.objects.get(id__exact=car_id)
        args['driver_id'] = driver_id
    except:
        args['car'] = None
        args['driver_id'] = None


    if args['car'] is not None:
        template = Template.objects.get(template_default__exact=1)

        return render(request, template_page, args)
    else:
        return HttpResponseRedirect('/rentacar/list/')

def rentacar_booking(request):
template = Template.objects.get(template_default__exact=1)
template_page = template.template_alias + str("/rentacar/rentacar_booking_form.html")
menu_config_list = MenuItemRentacarList.objects.all()[0]
menu_config = MenuItemRentacarList.objects.get(id=menu_config_list.id)
args['main_menu'] = MenuMenu.objects.get(id__exact=template.template_main_menu_id)
args['menu_items'] = MenuItem.objects.filter(
    menu_item_menu=args['main_menu'],
    menu_item_published=1,
)
args['current_menu_item'] = menu_config.menu_item_rentacar_list_menu_item
all_modules = Module.objects.filter(
    module_show_option__exact='all',
    module_published=1
)
selected_modules = Module.objects.filter(
    module_show_option__exact='selected',
    module_published=1,
    module_menu_item=args['current_menu_item']
)
excluded_modules = Module.objects.filter(
    module_show_option__exact='except',
    module_published=1,
).exclude(
    module_menu_item=args['current_menu_item']
)
args['modules'] = list(chain(all_modules, selected_modules, excluded_modules))

        try:
            args['car'] = Car.objects.get(id__exact=request.POST.get('car_id'))
            args['driver'] = Driver.objects.get(id__exact=request.POST.get('driver_id'))
        except:
            args['car'] = None
            args['driver'] = None

        if args['car'] is not None:
            return render(request, template_page, args)
        else:
            return HttpResponseRedirect('/rentacar/list/')
    else:
        return HttpResponseRedirect('/')

模板

rentacar_book_form

                    <div class="row">
                        <div class="medium-6 small-12 columns">
                            <label>
                                Book Start Date <br>
                                <input type="date" name="book_booking_start_date" required>
                            </label>
                        </div>
                        <div class="medium-6 small-12 columns">
                            <label>
                                Book End Date <br>
                                <input type="date" name="book_booking_end_date" required>
                            </label>
                        </div>
                    </div>

                            <hr>
                            <input type="submit" class='button large primary' value="Book {{ car.car_brand.brand_name }} {{ car.car_name }}">
                        </div>
                    </div>

                    <input type="text" name="book_booking_car_car" value="{{ car.id }}" class="hidden">
                    <input type="text" name="book_booking_driver_driver" value="{{ driver.id }}" class="hidden">

rentacar_car_car

        <form action="/rentacar/book-a-car/" method="post">
            {% csrf_token %}
            <input name="car_id" type="text" value="{{ car.id }}" class="hidden" required>
            <input name="driver_id" type = 'text' value="{{ driver.id }}" class = 'hidden' required>
            <button type="submit" class="button primary uppercase centered full-width">
                Book now!
            </button>
        </form>

urls.py

url(r'^rentacar/book-a-car/$', extension_views.rentacar_booking),
url(r'^rentacar/list/(\d+)/$', extension_views.rentacar_list),
url(r'^rentacar/car/(\d+)/driver/(\d+)/$', extension_views.rentacar_car),

1 个答案:

答案 0 :(得分:0)

如果您只想从隐藏的输入中获取值,请执行此操作。 在方法rentacar_car中输入:

car_id = request.GET.get('car_id')
driver_id = request.GET.get('driver_id')
print(car_id)
print(driver_id)

并且由于您要从这些输入中获取值,因此必须将表单方法更改为GET。