Django How do i pass the url parameter with the form button?

Django How do i pass the url parameter with the form button?



Hi i am Newbie in django i read django documentation but i had made a url with parameteres i want to book a car . for booking i need car id and driver id . after redirect to my booking page . my book now button is not sending the driver id and car id . please help me.
for example.
i am having a John as a Driver , and he click on Audi To book . after getting his url i want to save it to my booking database . after click on button in my html "book now" it doesnt render to next page book a car and dont get the car id and driver id.
please help me.



Sorry for my english . if didnt understand please let me know in comments ill more explain to you and share more content . i am just beginner in field so just learning .



Views


@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('/')



Templates



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),





args is not defined in rentacar_booking and rentacar_car
– gogaz
Aug 31 at 13:12



args


rentacar_booking


rentacar_car





@gogaz i didnt understand
– Abu bakr
Aug 31 at 13:14





When you do args['car'] = Car.objects..., the args variable is not yet defined. It may be due to the formatting you applied when posting to SO
– gogaz
Aug 31 at 13:15



args['car'] = Car.objects...


args





@gogaz see my code now i updated it .i had removed it because it was to long .
– Abu bakr
Aug 31 at 13:19






ok, then you're not showing us your code correctly, because what you have posted here cannot run, it would lead to a python NameError: name 'args' is not defined. Please always make sure you post relevant code that people can use to reproduce your issue. Leave out whatever is not relevant, but what you post must be complete in a programming sense.
– dirkgroten
Aug 31 at 13:42


NameError: name 'args' is not defined.




1 Answer
1



If you just want to get the values from hidden inputs just do this.
In method rentacar_car type:


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



And since you are getting the values from those inputs you have to change method of form to GET.





its not working and not a solution .
– Abu bakr
Aug 31 at 14:08





stackoverflow.com/questions/150505/… then have a look at this
– Dabux
Aug 31 at 14:11





what error does it outputs, if it isnt working?
– Dabux
Aug 31 at 14:14





after click on Book now button its not redirect me to the page book-a-car
– Abu bakr
Aug 31 at 14:30





so you printed driver_id and car_id and those numbers are wrong? That was your question before, but if they are correct, you have the error somewhere else.
– Dabux
Aug 31 at 14:47



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)