0

My django views look like following:

#view to retrieve all the exercises
def exercise_all(request):
"""
List all exercises.
"""
if request.method == 'GET':
    exercises = Exercise.objects.all()
    serializer = ExerciseSerializer(exercises, many=True)
    return JSONResponse(serializer.data)


#view to retrieve initial default list of exercises, this will be called on angular controller load
def exercise_initial(request):
"""
Gives the first exercise.
"""
if request.method == 'GET':
    exercises = Exercise.objects.filter(exercise_type=1 , intensity_level=1)
    serializer = ExerciseSerializer(exercises, many=True)
    return JSONResponse(serializer.data)


#view to retrieve all specific exercises
def exercise_next(request, exType, level):
"""
Gives the next exercise.
"""
if request.method == 'GET':
    exercises = Exercise.objects.filter(exercise_type=exType, intensity_level=level)
    serializer = ExerciseSerializer(exercises, many=True)
    return JSONResponse(serializer.data)  

My url.py:

 url(r'^exercises/all/$', views.exercise_all),
 url(r'^exercises/initial/$', views.exercise_initial),
 url(r'^exercises/(?P<exType>\d)/(?P<level>\d)/$', views.exercise_next), 

My angular Controller that call these views:

.controller('videoCtrl', function($scope,$http, $sce, $ionicPopup, djangoAuth) {

$scope.exercise_type='';
$scope.exercise_description='';
$scope.exercise_video_url='';

/**  To fetch the initial exercise **/
$http.get("https://localhost:8000/api/exercises/initial/").then(function(response){

  $scope.initial_data=response.data;

  angular.forEach($scope.initial_data, function(item){

               $scope.exercise_type=item.exercise_type;
               $scope.intensity_level=item.intensity_level;
               $scope.exercise_description=item.description;
                  $scope.exercise_video_url=$sce.trustAsResourceUrl(item.video_url);
           })
});

//Function to play next video
$scope.playNextTest = function(){

  alert("Next exercise: " + ($scope.exercise_type+1)); //demo line, logic needs to be implemented here     
}

What I want to achieve:

  • On initial video controller load I can fetch the initial exercise details which I assign to $scope parameters as can be seen in the code snippet.
  • When I run playNextTest() function I should be able to call django view exercise_next for which I need to modify my url with appending something like ../api/exercises/($scope.exercise_type+1)/(($scope.intensity_level)/

How can this be achieved?

2
  • Are you saying that your current controller does not get the data? What's the problem exactly? You just need to do another http.get on your next video function. Again, what's the problem? Commented Mar 25, 2016 at 15:00
  • Yes my question is how should I do $http.get for the second time when I call playNext(), there I need to insert two parameters in the request which will look like api/exercises/($scope.exercise_type+1). How can I modify the url in a correct way? To include these scope parameters. Commented Mar 26, 2016 at 8:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.