在使用“ django-recaptcha”验证Recaptcha之后如何将页面重定向到其他页面

时间:2018-09-23 14:10:11

标签: python django django-recaptcha

我正在从事这个项目:https://github.com/jypsi/opencabs。我已经在预订页面中集成了django-recaptcha(https://github.com/praekelt/django-recaptcha)。但是,当Recaptcha验证通过后,页面不会继续进行,这意味着它不会将我重定向到下一页。在views.py页面中需要做一些更改吗?但是,当我注释掉验证码字段页面代码(Booking.py)时,它会将我重定向到下一页。请帮忙。

Booking.py

class BookingTravelForm(BaseBookingForm):
captcha = ReCaptchaField(
    public_key="6LccU20UAAAAAI1twyn3TZT-feMSdy-U-I7iGFIn",
    private_key="my private key",
)

Views.py

FORMS = [
('itinerary', booking_form.BookingTravelForm),
('vehicles', booking_form.BookingVehiclesForm),
('contactinfo', booking_form.BookingContactInfoForm)
]

TEMPLATES = {
'itinerary': 'opencabs/index.html',
'vehicles': 'opencabs/booking_vehicles.html',
'contactinfo': 'opencabs/booking_contactinfo.html'
}


class BookingWizard(CookieWizardView):

    form_list = FORMS

    def get_context_data(self, form, **kwargs):
        context_data = super().get_context_data(form, **kwargs)
        context_data['settings'] = settings
        return context_data

    def get_form_kwargs(self, step):
        data = {}
        if step == 'vehicles':
            itinerary_data = self.get_cleaned_data_for_step('itinerary')
            data.update({'source': itinerary_data['source'],
                     'destination': itinerary_data['destination'],
                     'booking_type': itinerary_data['booking_type']})
        return data

    def get_template_names(self):
    return [TEMPLATES[self.steps.current]]

def done(self, form_list, form_dict, **kwargs):
    data = form_dict['itinerary'].cleaned_data
    data.update(form_dict['vehicles'].cleaned_data)
    data.update(form_dict['contactinfo'].cleaned_data)
    booking = Booking(**data)
    booking.save()
    try:
        msg = ("Dear customer,\n"
               "We've received your booking request with ID: {}\n"
               "You'll receive a notification when your booking "
               "is confirmed!").format(booking.booking_id)
        if booking.customer_mobile:
            send_sms([booking.customer_mobile], msg)
        if booking.customer_email:
            send_mail('Booking request received', msg,
                      settings.FROM_EMAIL, [booking.customer_email])
    except Exception as e:
        print("SMS Error: %s" % e)
    return redirect(
        reverse('booking_details') + '?bookingid=' + booking.booking_id)

booking_wizard = BookingWizard.as_view()


def index(request):
    return render(request, 'opencabs/index.html', {
    'settings': settings,
    'wizard': booking_wizard
    })


def booking_details(request):
    booking_id = request.GET.get('bookingid', '').upper()
    booking = get_object_or_404(Booking, booking_id=booking_id)
    return render(request, 'opencabs/booking_details.html', {
    'settings': settings,
    'booking': booking
})


@staff_member_required
def booking_invoice(request, booking_id):
    booking = Booking.objects.get(id=booking_id)
    path = booking.invoice()
    with open(path, 'rb') as f:
        response = HttpResponse(
        content=f.read(), content_type='application/pdf')
    os.remove(f.name)
        return response

0 个答案:

没有答案