【问题标题】:Django Angularjs How to call Http request with specific IdDjango Angularjs如何调用具有特定ID的Http请求
【发布时间】:2014-08-13 08:41:43
【问题描述】:

我是 angularjs 的新手。我正在尝试使用 angularjs 在我的 django 模板之一中提取数据。这是我实际尝试提取数据的视图。

def download_history(request,download_id):
    from django.core import serializers
    download_history = DownloadHistory.objects.filter(user = request.user,pk=download_id)
    ctx_detail_history = serializers.serialize('json', download_history)

    return HttpResponse(ctx_detail_history)

这是网址

url(r'^history_detail/(?P<download_id>\d+)/$',views.download_history,name = 'download_history'),

这是我的模板,我试图在其中提取数据并尝试在 jquery 弹出模式中显示。

{% extends 'base.html' %}
{% block title%}User Subscription {%endblock%}
{%block content%}

<div style="margin-top:100px;background-color:#85ADFF;" ng-app='history'>

    <table class="table table-striped" >
        {%for download_history_name in download_history%}
         <tr>

          <td><button type='button' data-toggle="modal" data-target="#myModal"><a href={%url 'download_history' download_history_name.id %} >{{download_history_name.name}} and the id {{download_history_name.id}}</a></button></td>
         </tr>
        {%endfor%}
</table>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Objact Name</h4>
      </div>
      <div class="modal-body">
         <div class="table-responsive">
          <table class="table table-striped" ng-controller='HistoryCtrl'>

            <tbody>
                <tr>
                    {[{detail_history.fields.name}]}
                    {[{detail_history.fields.download_rate}]}
                    {[{detail_history.fields.photo.url}]}
                    {[{detail_history.fields.downloaded_time}]}
                    {[{detail_history.fields.DuePayment}]}
                </tr>
            </tbody>

          </table>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>




</div>  

{%block extrajs%}
<script>

var app = angular.module('history',[]);
app.config(function($httpProvider){
   $httpProvider.defaults.xsrfCookieName = 'csrftoken';
   $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';

});

app.config(function($interpolateProvider){
   $interpolateProvider.startSymbol('{[{');
   $interpolateProvider.endSymbol('}]}');

});

app.controller('HistoryCtrl',['$scope','$http' , function($scope,$http){
    $http.get('/history/history_detail/(? P<download_id>\d+)/').success(function(data){
          //$scope.doToggle=false;
          $scope.detail_history=data;
        }).error(function(data,status){
              $scope.data = data || 'Request Fails';
              $scope.status = status;
              console.log(data)
            });

  }]);
</script>


{%endblock%}

{%endblock%}

现在的问题是,虽然我试图用特定的 id 提取数据,但它不起作用,事实上,如果我检查页面,它会显示返回 URL 的东西 p>

   http://localhost:8000/history/history_detail/(?Pd+)/

现在是什么问题,我肯定在这里犯了一个错误,但是我是angularjs的新手,无法弄清楚。是否可以使用ng-click功能解决问题?

【问题讨论】:

    标签: javascript jquery django angularjs


    【解决方案1】:

    通过对 JavaScript 的粗略检查,您似乎正在尝试向包含正则表达式的 URL 发出 GET 请求。这对您的 Django 配置很有意义,因为您告诉应用程序在您分配给名为 download_id 的变量的 url 中期望一个数字。您似乎已将此 URL 直接复制到您的 JavaScript 中,实际上您应该在 GET 请求中提供特定的下载 ID(例如“/history/history_detail/12345/”)。

    【讨论】:

    • id 会在事件发生后从锚标签中检索 ..
    • @Tanweer 除非它不会,因为你还没有编写任何代码来做到这一点。
    猜你喜欢
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    相关资源
    最近更新 更多