3

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),
Abu bakr
  • 139
  • 3
  • 13
  • `args` is not defined in `rentacar_booking` and `rentacar_car` – gogaz Aug 31 '18 at 13:12
  • @gogaz i didnt understand – Abu bakr Aug 31 '18 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 '18 at 13:15
  • @gogaz see my code now i updated it .i had removed it because it was to long . – Abu bakr Aug 31 '18 at 13:19
  • Same problem, your first line with `args` is `args['main_menu'] = MenuMenu.objects.get(id__exact=template.template_main_menu_id)` which cannot work because `args` isn't defined anywhere. – dirkgroten Aug 31 '18 at 13:24
  • @dirkgroten please tell me the solution . i dont know how to do > – Abu bakr Aug 31 '18 at 13:26
  • Also in general, a Django view for a form has two cases: The GET case and the POST case. The GET case shouldn't redirect but render the empty form. The POST case checks if the submitted form is valid, then redirects when this is the case (success) or re-renders the bound form (this time with the values the user already entered) so the user can correct the errors. I'm saying this because you shouldn't return `else: return HttpResponseRedirect('/')` in your `rentacar_booking` view – dirkgroten Aug 31 '18 at 13:29
  • Abu, this is basic python, I'd suggest you lookup some python tutorial on how to use dictionaries, for example [this one](https://www.tutorialspoint.com/python/python_dictionary.htm) – dirkgroten Aug 31 '18 at 13:30
  • @dirkgroten what should i do then for my view. can you help me to give me the answer . – Abu bakr Aug 31 '18 at 13:30
  • At this point, I'd suggest you take a step back and try to really understand Django by taking dome time to do the [Django Tutorial](https://docs.djangoproject.com/en/2.1/intro/tutorial01/) and probably some python classes. There are two many issues with your code to be fixed with a simple answer, sorry to say. – dirkgroten Aug 31 '18 at 13:35
  • @dirkgroten but my car id is passed when i click on book a car my car id is passed but my driver is not pass. this is just a problem i am getting now . – Abu bakr Aug 31 '18 at 13:37
  • 1
    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 '18 at 13:42
  • You're putting `{{ driver.id }}` in your template but `driver` isn't in your template's context (you're passing `driver_id`). – dirkgroten Aug 31 '18 at 13:42

1 Answers1

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.

Dabux
  • 123
  • 10
  • https://stackoverflow.com/questions/150505/capturing-url-parameters-in-request-get?rq=1 then have a look at this – Dabux Aug 31 '18 at 14:11
  • after click on Book now button its not redirect me to the page book-a-car – Abu bakr Aug 31 '18 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 '18 at 14:47