gpt4 book ai didi

Django 如何使用表单按钮传递 url 参数?

转载 作者:行者123 更新时间:2023-12-02 04:27:53 25 4
gpt4 key购买 nike

嗨,我是 django 的新手,我阅读了 django 文档,但我已经制作了一个带有参数的 url,我想预订一辆车。预订时我需要汽车身份证和司机身份证。重定向到我的预订页面后。我的立即预订按钮未发送司机 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),

最佳答案

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

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

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

关于Django 如何使用表单按钮传递 url 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52116133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com