Page not loading even after 200 status

Viewed 98

I'm trying to redirect to the invoice page but nothing changes except the refresh

this is the url.py

urlpatterns = [
    path('', User_login_view, name='login'),
    path('home/', HomePageView.as_view(), name='home'),
    path("nglm/", NGLMView.as_view() , name="nglm"),
     """ other urls for the sidebar"""
    path("createinvoice/", NGLINVAPI.as_view(), name="createinvoice"),
    path("nglinvoice/", NGLInvoiceTV.as_view(), name="nglinvoice")
    ]

After login I go to "nglm" page where I input the customer details and click the submit button which calls the api "createinvoice". the class is shown below

class NGLINVAPI(APIView):
def post(self,request):
    if 'nglinv' == request.POST['formtype'] :
        data = {
            'name': request.data['cname'],
            'civilid': request.data['civilid'],
            'ph':request.data['ph'],
            'disc':request.data['disc'],
            'bookno':request.data['Bookno'],
            'tga':request.data['LAmount'],
        }
        p,ccreated = update_customerdb(data=data)
        l = update_gldb(data=data)
        data['cid'] = p.id
        iq,i = update_inventory(data=data,gl=l)
        lici = update_LICI(cid=p,lid=l,data=data)
        ninv = update_invoice(licid=lici,lid=l,cid=p,data=data,il = iq,items = i)
        invid = ninv.id
        NGLItemModel.objects.all().delete()
        base_url = '/nglinvoice/'
        query_string =  urlencode({'pk': ninv.pk}) 
        url = '{}?{}'.format(base_url, query_string)
        return redirect(url)    
    return HttpResponseRedirect('home')

this should have redirected me to invoice page "nglinvoice". the view is show below

class NGLInvoiceTV(TemplateView):
template_name = "pages/nglinvoice.html"
def get(self, request):
    p = request.GET['pk']
    inv = get_object_or_404(InvoiceModel, pk=p)
    context = {}
    context['segment'] = 'api'
    context['pagename'] = 'New GL API'
    context['object'] = inv
    
    return render(request,self.template_name , context)

I have no issues up to this point. the problem is it doesn't show the page "nglinvoice.html", it just refresh the "nglm" page

below is what the terminal is showing:

System check identified no issues (0 silenced).
May 15, 2022 - 16:47:28
Django version 3.2.9, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[15/May/2022 16:47:53] "GET / HTTP/1.1" 200 4103
[15/May/2022 16:47:55] "POST / HTTP/1.1" 302 0
[15/May/2022 16:47:55] "GET /home/ HTTP/1.1" 200 23840
[15/May/2022 16:47:58] "GET /nglm/ HTTP/1.1" 200 24028
[15/May/2022 16:47:58] "GET /itemlist/ HTTP/1.1" 200 2
[15/May/2022 16:48:02] "POST /additem/ HTTP/1.1" 302 0
[15/May/2022 16:48:02] "GET /nglm/ HTTP/1.1" 200 24028
[15/May/2022 16:48:02] "GET /itemlist/ HTTP/1.1" 200 2
[15/May/2022 16:48:23] "POST /additem/ HTTP/1.1" 200 8
[15/May/2022 16:48:23] "GET /itemlist/ HTTP/1.1" 200 104
[15/May/2022 16:48:34] "GET /apicheck/?term=p HTTP/1.1" 200 17
[15/May/2022 16:48:35] "GET /get_customerdetails/?k=PETER+JOHNSON&t=n HTTP/1.1" 200 102<br>
[15/May/2022 16:48:38] "POST /createinvoice/ HTTP/1.1" 302 0
[15/May/2022 16:48:38] "GET /nglinvoice/?pk=5 HTTP/1.1" 200 13863

if I enter the last url manually, I can see the page but if i click the submit button it just refresh the page

how can i go to the invoice page after the clicking the submit button

2 Answers

Side note, too long for comment.

URL dispatching is done in non-Django way, in non-REST way. Why /nglinvoice/?pk=5 ? This leads to extra "look around" code reinventing built-in django features available out of the box.

Instead of this:

# urls
urlpatterns = [
    path("nglinvoice/", NGLInvoiceTV.as_view(), name="nglinvoice")
    ]

# view
base_url = '/nglinvoice/'
query_string =  urlencode({'pk': ninv.pk}) 
url = '{}?{}'.format(base_url, query_string)

try this:

# urls
urlpatterns = [
    path("nglinvoice/<int:pk>/", NGLInvoiceTV.as_view(), name="nglinvoice")
    ]

# view
url = reverse('nglinvoice', args=(ninv.pk, ))

here is an example from the docs specifically with redirect.

Instead of generic TemplateView try more specific and related to what is going on in your code DetailView. It will automatically get requested record.

I posted this on Django Forum as well and was suggested force JavaScript to redirect instead from the APIView and it started to work

https://forum.djangoproject.com/t/page-not-loading-even-after-200-status/13777/4

So I made a few changes

In my view:

class NGLINVAPI(APIView):
 def post(self,request):
    if 'nglinv' == request.POST['formtype'] :
        """Blah Blah Blah """
        url = url = reverse('nglinvoice', args=(ninv.pk, ))
        return JsonResponse({'url':url},safe=False)    
    return HttpResponseRedirect('home')

and in my ajax success

        success: function (response) {
        window.location.href = response.url          
    },
Related