1

I have created a backend using Django REST and for handling API's at frontend I am using Angular (I have just started using angular).

I have one initial page let say User Profile Page So If user is authenticated I wants to show His profile on that initial page. All Authentication related thing is handled in API's So I am just getting response and redirection to profile page or login page is handled via Django.

I can get user related data in django and render that to HTMl page but in that case Angular functions cannot serve this django data.

So what will be the best way that whenever this profile page url is loaded an API call will be made that will fetch user data and angular can show that data in template and will be able to use it further ?

EDIT

let say you open this url -> /user-profile/ which refer to below View

class HomeView(TokenRequiredMixin, TemplateView):
    """
    """
    template_name = 'portal/index.html'
    mixin_failed_url = reverse_lazy('login-view')

It basically render to profile apge , here in this view I can fetch the user data and show them on HTML but angular will not be serve in this case.

One way to do this call any angular function using ng-init and load user data that will server the angular issue but its not the best way to do I guess.

user5594493
  • 1,050
  • 3
  • 10
  • 24
  • 2
    Your question is too broad. Please narrow it down and provide some code examples from your current situation. What did you already try? – Sander van Leeuwen Nov 24 '15 at 09:21
  • @SandervanLeeuwen I have edited my question , It makes any sense now ? – user5594493 Nov 24 '15 at 09:28
  • You'll need to serve ajax responses from django. Then see this answer http://stackoverflow.com/a/13031318/948323 on how to load it in Angular. – Sander van Leeuwen Nov 24 '15 at 09:33
  • @SandervanLeeuwen But don't you think the request he made in controller will be called every time the controller is initialized , I mean to say when any page is loaded the controller is initialized and hence that $http request will be made, I need the similar kind of solution but for s particular url or a case . – user5594493 Nov 24 '15 at 09:38
  • That's what routing is for: https://docs.angularjs.org/tutorial/step_07. Use a HomeController which is only initialized on '/' or '/profile/'. Consider ui-router for advanced routing. – Sander van Leeuwen Nov 24 '15 at 09:43
  • @SandervanLeeuwen Thanks , just last question , I saw this routing includes `templateUrl` and you know we serve templates using Views , So here How can we server them by calling a djnago url which further calls a template ? – user5594493 Nov 24 '15 at 09:48
  • Django should only serve json responses, no HTML. Angular is responsible for the presentation and should render a template with the loaded json data from the Django backend. Look at this example on how to create an Ajax view in Django: https://docs.djangoproject.com/en/1.8/topics/class-based-views/generic-editing/#ajax-example – Sander van Leeuwen Nov 24 '15 at 10:52

0 Answers0