0

I have created a Django rest framwork back-end guided by the tutorial at:

http://www.django-rest-framework.org/

I have uploaded the back-end to:

https://github.com/jakkan/grader

I want to build an Angular front-end to the djano-rest-framework back-end. I have pretty good knowledge of Angular in general but don't know how to create a front-end for logging in and out.

I hope that this is not a too big question, but how can I add an Angular front-end for logging in and out?

2
  • I think you just need to use rest end point in your angular using $http or $resource. Commented Jan 20, 2016 at 6:00
  • Thank you! Can you post an example as solution? That wouls be very helpful. I have a hard time understanding how to do it Commented Jan 20, 2016 at 6:08

1 Answer 1

2

Once you have created you rest api using django-rest-framework then you will many end points as you may had created and configured into urls.

So to integrate the rest api, First of you need to have access to the domain where your rest api are exposing, we will assume localhost:8000.

Now you should create an angular constant to hold this rest endpoint domain information, because we will use many api at many places in our angular project, So in order to keep it clean we must use constant and this can be inject wherever we require rest endpoint.

Rest Endpoint constant in AgnularJS

angular.module('angularApp')
  .constant('restEndPoint', 'http://localhost:8000');

Now this will be avaialbe anywhere in project by injecting such as a service.

It depends how you design your rest api but If you have design your rest api with rest standards then I would suggest to use $resource instead of using $http.

I always prefer to write my persistent logic inside a factory, Again its your choice.

Here is an example of using the rest endpoint in angular's way.

Factory

angular.module('AngularApp')
  .factory('TodoService', ['$resource', 'restEndPoint', function  ($resource, restEndPoint) {
    return $resource(restEndPoint+'/todos/:id')
}]);

Controller

angular.module('AngularApp')
  .controller('TodoCtrl', function ($scope, TodoService) {
      var Todo = new TodoService();

      // Now Todo is an $resource instance. Which can be used to get/post/patch/delete data on rest api. such as following.

     // GET all records.
     Todo.$query(); 

     // GET specific record based on id.
     Todo.$query({id:1});

     // POST data to create new Todo item.
      newTodo = { title: 'Task 1', completed: false}
      Todo.$save(newTodo)

     // PUT/PATCH specific record base on id.
     editTodo =  { title: 'Task title changed', completed: false}
     Todo.$update({id: 1}, editTodo); 

});

I hope you understand the rest api integration with angularJS. Let me know if you have any queries.

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

@user1283776 Let me know if its useful and let other know as well by accepting the answer.

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.