python - Send data from django views to angularjs controller -


i trying send data django views.py file angularjs controller.js file unable send it. problem unable send data views file controller. , when run code value {%steps%} doesn't change value have assigned in views file. don't want use django rest framework doing this. there other way of achieving this? if yes, please me out.

views.py:

from django.shortcuts import render django.conf import settings user_data.models import getdata import json  def home(req):     return render(req, 'index.html')  def userdata(req):     if req.method == 'get':         user_data = getdata().getalldata()         context_dict = {'user_data1' : json.dumps(user_data[0]),                         'user_data2' : json.dumps(user_data[1]),                         'user_steps' : json.dumps(2000)}          return render(req, 'index.html', context_dict) 

controller.js :

'use strict';  metronicapp.controller('dashboardcontroller', function($rootscope, $scope, $http, $timeout) {     $scope.$on('$viewcontentloaded', function() {            // initialize core components         metronic.initajax();     });     $scope.steps = {{user_steps|safe}}; }); 

html file:-

<div ng-controller="dashboardcontroller" class="margin-top-10">     <div class="row ">                 <div class="col-md-12 col-sm-12">                     <div class="portlet light ">                             <div class="portlet-title">                                 <div class="caption">                                     <i class="icon-cursor font-purple-intense hide"></i>                                     <span class="caption-subject font-purple-intense bold uppercase">tracker report</span>                                 </div>                                 <div class="actions">                                     <a href="javascript:;" class="btn btn-sm btn-circle btn-default easy-pie-chart-reload">                                     <i class="fa fa-repeat"></i> reload </a>                                 </div>                             </div>                             <div class="portlet-body">                                 <div class="row">                                 <div class="col-md-3">                                     <div class="easy-pie-chart">                                         <div class="number transactions" data-percent="55">                                             <span>{$ steps $}</span>                                         </div>                                         <!-- <a class="title" href="#"> -->                                         steps <!-- <i class="icon-arrow-right"></i> -->                                         </a>                                     </div>                                 </div>                                 <div class="margin-bottom-10 visible-sm">                                 </div>                                 <div class="col-md-3">                                     <div class="easy-pie-chart">                                         <div class="number visits" data-percent="85">                                             <span>                                             +85 </span>                                             %                                         </div>                                         <!-- <a class="title" href="#"> -->                                         sleep<!-- <i class="icon-arrow-right"></i> -->                                         </a>                                     </div>                                 </div>                                 <div class="margin-bottom-10 visible-sm">                                 </div>                                 <div class="col-md-3">                                     <div class="easy-pie-chart">                                         <div class="number bounce" data-percent="46">                                             <span>                                             +46 </span>                                             %                                         </div>                                         <!-- <a class="title" href="#"> -->                                         calories <!-- <i class="icon-arrow-right"></i> -->                                         </a>                                     </div>                                 </div>                                 <div class="margin-bottom-10 visible-sm">                                 </div>                                 <div class="col-md-3">                                     <div class="easy-pie-chart">                                         <div class="number bounce" data-percent="32">                                             <span>                                             +32 </span>                                             %                                         </div>                                         </a>                                     </div>                                 </div>                              </div>                             </div>                         </div>                 </div> </div> 

this have done in 1 of major django projects (but not 100% sure, whether or ain't looking for, answer). so, instead of views.py, have made custom.py file in make custom apis , call them (using urlpatterns) in urls.py of django app. , have set of apis using django rest framework, make viewsets rather simple views. in case, interested, might reading this link.

but since, mentioned don't want use django rest frameworks, give example of custom.py file, mentioned above. below find sample of api defined in custom.py,

@api_view(['get']) def get_user_details(request):     """     api view gives user detail      ---      parameters:         - name: email           description: email of user based on his/her information has been extracted           required: true           type: string      responsemessages:         - code: 400           message: email required parameters.           message: user not found.         - code: 200           mesage: user details sent      consumes:         - application/json         - application/xml     produces:         - application/json         - application/xml      """      email = request.query_params.get('email', none)      if email none:         return httpresponsebadrequest("email required parameters.")      try:         user = user.objects.get(username=email)     except user.doesnotexist:         return httpresponsebadrequest("user not found.")      response_data = {'id': user.id, 'first_name': user.first_name, 'last_name': user.last_name,}      return httpresponse(json.dumps(response_data), content_type="application/json") 

and subsequently, urls.py in django app looks like:

urlpatterns = router.urls  urlpatterns = urlpatterns + [     url(r'get_user_details/', get_user_details), ] 

my controller this:

  checkemail : function (current) {     return $http({         method: 'get',         url: baseapi + 'admin/get_user_details/',         params: {email: current},     })   }, 

and subsequently, may able render variable wish print in html file using handles.

hope, helps.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -