即使在 200 状态后页面仍未加载

Page not loading even after 200 status

我正在尝试重定向到发票页面,但除了刷新之外没有任何变化

这是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")
    ]

登录后,我进入“nglm”页面,输入客户详细信息,然后单击调用 api“createinvoice”的提交按钮。 class 如下所示

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')

这应该将我重定向到发票页面“nglinvoice”。视图如下所示

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)

到目前为止我没有任何问题。问题是它不显示页面“nglinvoice.html”,它只是刷新“nglm”页面

以下是终端显示的内容:

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

如果我手动输入最后一个 url,我可以看到该页面,但如果我单击提交按钮,它只会刷新页面

点击提交按钮后如何进入发票页面

旁注,太长无法评论。

URL 调度以 non-Django 方式完成,以 non-REST 方式完成。为什么 /nglinvoice/?pk=5 ?这会导致额外的“环顾四周”代码重塑 built-in 开箱即用的 django 功能。

而不是这个:

# 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)

试试这个:

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

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

这里是来自文档的 example,特别是 redirect

不要使用通用的 TemplateView,而是尝试更具体且与代码中发生的事情相关 DetailView。它会自动获取请求的记录。

我也在 Django 论坛上发布了这个,有人建议强制 JavaScript 重定向而不是从 APIView 开始工作

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

所以我做了一些改变

在我看来:

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')

并且在我的 ajax 成功中

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