So I start on the post_ad()
view. When I submit boost_form
, the view successfully changes to options()
- where the template changes to advertising/options.html
. However the page URL stays the same (/advertise/post/
).
When I perform my ajax function, it successfully fires the first ajax call (if request.POST.get('position'):
) and prints the relevant variables.
I then have another ajax call (an onsubmit
for a form) which then calls the 2nd ajax in my options()
view: if request.POST.get('confirmed') == 'confirmed'
. This succesfully fires, and the print statements in my pay()
view fire, however my terminal doesn't show the POST
request for pay()
:
Position: 8 Duration: 106 Total price: 63
[10/Apr/2018 10:46:38] "POST /advertise/options/ HTTP/1.1" 200 13484
the final view
Pay
Pay ajax
[10/Apr/2018 10:46:40] "POST /advertise/options/ HTTP/1.1" 200 11615
and subsequently doesn't load the pay.html
template. Can somebody tell me why this is?
Here's my code:
def post_ad(request):
boost_form = AdvertisePostForm(request.POST or None, request.FILES or None)
if boost_form.is_valid():
instance = boost_form.save(commit=False)
if Post.objects.filter(hash=instance.hash).exists():
instance.hash = random_string()
instance.ad = True
instance.save()
return options(request)
context = {
'boost_form': boost_form
}
return render(request, 'advertising/post_ad.html', context)
def options(request):
latest = AdvertisePost.objects.order_by('-id')[0]
ad = get_object_or_404(AdvertisePost, hash=latest.hash)
if request.is_ajax():
if request.POST.get('position'):
position = request.POST.get('position')
duration = request.POST.get('duration')
total_price = request.POST.get('total_price')
ad.position = position
ad.duration = duration
ad.total_price = total_price
ad.save()
print('Position:', position, 'Duration:', duration, 'Total price:', total_price)
if request.POST.get('confirmed') == 'confirmed':
print('the final view')
return pay(request)
context = {
'ad': ad
}
return render(request, 'advertising/options.html', context)
def pay(request):
print('Pay') #prints
if request.is_ajax():
print('Pay ajax') #prints
return render(request, 'advertising/pay.html', {})
my template
<form method="post" enctype="multipart/form-data" autocomplete="off" id="add_num_div">{% csrf_token %}
...
<button class="submit_medium" type="submit" id="submit_boost">Next</button>
</form>