0

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>
Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • Calling one view function from another is not the same as navigating from one page to another. This won't fire a new request. Also, you cannot redirect during an ajax call (It is an asynchronous request). You will have to send new url back to javascript and set new url on ajax callback – Ramkishore M Apr 10 '18 at 11:04
  • `You will have to send new url back to javascript and set new url on ajax callback`: can you elaborate on what this means exactly? - I just changed the url on my ajax call to `/advertise/pay/` - and it successfully calls `pay()` but the page doesn't change. – Zorgan Apr 10 '18 at 11:12
  • https://stackoverflow.com/questions/18118627/redirecting-after-ajax-post/18118675 – Ramkishore M Apr 10 '18 at 11:35
  • `window.location.href = 'URL WOULD BE HERE'` – Anup Yadav Apr 10 '18 at 11:54
  • @AnupYadav thanks - is it possible to pass any parameters to that url? – Zorgan Apr 10 '18 at 12:14
  • Yes, why not? you can add params to query string using ? and & But you need to add those into urls / slug – Anup Yadav Apr 10 '18 at 12:30

0 Answers0