2016-09-15 161 views
0

我正在研究一个python django web应用程序,我想在其中实施国际化并将整个应用程序自动转换为法语或中文。查看未返回响应

我参考了从该网站https://www.metod.io/en/blog/2015/05/05/django-i18n-part-1/

但每当我尝试运行应用程序它显示了这个错误:

​​

和URL get_dashboard_data通过AJAX获取数据。

url(r'^get_dashboard_data/$', views.getDashboardData, name='getDashboardData'), 

视图

@login_required(login_url='/') 
def getDashboardData(request): 
    dbname = request.user.username 
    if request.method == 'POST' and request.is_ajax(): 
     if request.POST.get('action') == 'sale_chart_data': 
      data = DashboardData(dbname).getSaleChartData() 
      channel_list = data[0] 
      data_list = data[1] 
      print 123, data_list, channel_list 
      return HttpResponse(json.dumps({'channel_list':channel_list, 'data_list':data_list}), content_type='application/json') 

     if request.POST.get('action') == 'get_sale_numbers': 
      sale_data = DashboardData(dbname).getSaleNumbers() 
      return HttpResponse(json.dumps({'sale_number_data':sale_data}), content_type='application/json') 

     if request.POST.get('action') == 'get_inventory_numbers': 
      inventory_data = DashboardData(dbname).getInventoryData() 
      return HttpResponse(json.dumps({'inventory_data':inventory_data}), content_type='application/json') 

     if request.POST.get('action') == 'get_order_numbers': 
      order_data = DashboardData(dbname).getOrderData() 
      return HttpResponse(json.dumps({'order_data':order_data}), content_type='application/json') 

     if request.POST.get('action') == 'get_hourly_data': 
      order_data = DashboardData(dbname).getHourlyData() 
      sale_data = order_data[1] 
      count_data = order_data[0] 
      return HttpResponse(json.dumps({'sale_data':sale_data, 'count_data':count_data}), content_type='application/json') 

     if request.POST.get('action') == 'top_performers': 
      data = DashboardData(dbname).getTopPerformers() 
      inventory_count_dict = data[0] 
      current_month_dict = data[1] 
      last_month_dict = data[2] 
      current_quarter_dict = data[3] 
      current_year_dict = data[4] 
      channel_list = data[5] 
      return HttpResponse(json.dumps({'inventory_count_dict':inventory_count_dict,'current_month_dict':current_month_dict,'last_month_dict':last_month_dict,'current_quarter_dict':current_quarter_dict,'current_year_dict':current_year_dict,'channel_list':channel_list}), content_type='application/json') 

     if request.POST.get('action') == 'top_products': 
      product_data = DashboardData(dbname).getTopProducts() 
      return HttpResponse(json.dumps({'product_data':product_data}), content_type='application/json') 

的JavaScript

function getSaleChart(){ 
    $.ajax({ 
     url : "/get_dashboard_data/", 
     type : "POST", 
     data : {action:'sale_chart_data'}, 

     success : function(response) { 
      channel_list = response.channel_list; 
      data_list = response.data_list; 
      c3.generate({ 
       bindto: '#sale-chart-30-days', 
       data:{ 
        x: 'dates', 
        xFormat: '%b %d', 
        columns: data_list, 
        colors:{ 
         Flipkart: '#1AB394', 
         Paytm: '#BABABA' 
        }, 
        type: 'bar', 
        groups: [ channel_list ] 
       }, 
       axis: { 
        x: { 
         type: 'timeseries' 
        } 
       } 
      }); 
     }, 

     error : function(xhr,errmsg,err) { 
      toastr["error"]("Something Broke.", "Oops !!!."); 
      console.log(xhr.status + ": " + xhr.responseText); 
     } 
    }); 
} 
+0

该错误与您在此显示的任何代码无关,应显示您的视图。或者只是看看它。确保该视图中的每条路径都返回一个响应。 – Sayse

+0

@Sayse实际上,如果我不包括i18n包,那么该应用程序工作得很好,并返回响应,但是当我添加i18n包时,它显示错误 – User0706

+0

它会工作正常,因为无论您所做的任何更改使视图通过视图逻辑采取不同的路线。 – Sayse

回答

3

这就是为什么你应该多练习防御性编程。虽然你坚持请求方法是POST,并且它是ajax,并且action是sale_chart_data,但三者之一不是你所期望的。

你的功能真的应该如下。这是一个很好的习惯。

def getDashboardData(request): 
    dbname = request.user.username 
    if request.method == 'POST' and request.is_ajax(): 
     action = request.POST.get('action') 
     if action == 'sale_chart_data': 
      data = DashboardData(dbname).getSaleChartData() 
      .... 
     ... 
     # other if conditions here 
     else : 
      return HttpResponse(json.dumps({'message':'Unknown action {0}'.format(action)}), content_type='application/json') 

    else : 
      return HttpResponse(json.dumps({'message':'Only ajax post supported'}), content_type='application/json') 

然后你应该设置断点并评估请求以确定在这个特定请求中究竟发生了什么。

+1

@Sayse是的,非常好的主意 – e4c5

+1

而不是一个普通的HttpResponse,我会建议返回一个[HttpResponseBadRequest](https://docs.djangoproject.com/en/) 1.10/ref/request-response /#django.http.HttpResponseBadRequest),以及坏请求。 –

+0

@DanielHepper对吧。如果我们想深入挖掘,那么我们也可以用JsonResponse替换HttpResponse。 – e4c5

0

我的猜测是你的JavaScript确实会向/get_dashboard_data/发出POST请求,但由于某种国际化中间件,它会收到一个重定向响应(HTTP 301或302)到/en/get_dashboard_data/

浏览器遵循重定向,但/en/get_dashboard_data/的新请求是GET请求。

编辑:

以下重定向时,浏览器将始终执行为获得第二个请求,没有办法防止。你有几个选择来解决这个问题:

  • 作出最初的请求到正确的应用程序。这意味着您必须将您的i18n网址传递到您的JavaScript中,而不是对其进行硬编码。您可以添加这样的事情到您的模板:

    <script>var dashBoardDataUrl = "{% url "name-of-dashboard-url" %}"</script>

  • 为您的“动作”刚刚得到的代码,你可以只接受一个GET请求,并读取查询paramters

  • 拆分查看操作到接受GET请求的几个较小的视图中,以便您拥有类似于REST API的内容。
+0

是的,您的新请求是GET请求。你可以建议如何解决这个问题? – User0706

+0

请看我编辑 –