How to fill a template without models? I use Django REST , python 3.4 , Angular JS .

Suppose there are 4 pages in the form of templates: main.html, page1.html, page2.html, page3.html .

The main page displays the list that is obtained from the database query. When you select any item, you must page1 , and display a list on it, also obtained from the results of the database query (the database is not Django), but taking into account the item selected on the main page, i.e. pass several parameters to REST , fill in the template page1.html results of the query and display the summary page. Further, when selecting an item, to carry out the same way to page2 , from it to page3 , etc.

Just make the links of the links like /page1/type=sometype&id=1 on the main page, on page1 - /page2/type=secondtype&id=6 etc? What type is better to use the class for processing requests - APIView ? Trying to use APIView , click on the link of the form /page1?id=1 Here is an example with no type parameter, only with id :

 class SomeAPI(APIView): permission_classes = [permissions.AllowAny] def get(self, request, *args, **kwargs): if (request.GET.get('id',None)): ... # result = получаю из базы данных набор записей на основе id return Response({'res':result}, template_name="page1.html") 

in urls.py: url(r'^page1'), SomeAPI.as_view()

In the page1.html template

 ... <tr ng-repeat="item in res"> {{item}} ... 

Records are returned correctly (that is, there is no problem getting data from the database), but they are not displayed in the page1.html template, but on the page of the standard REST API template as a JSON-структуры .

 { "res": [ { "item": "1" }, { "item": "2" }, { "item": "3" }, { "item": "4" } ] } 

In fact, I need to combine the functionality of TemplateView + APIView . It would be similar, as on the main page, from the AngularJS controller to request the APIView JSON result using the GET method, and fill in the template . But for page1, page2, page3 you need to use id . Passing it with a GET request is not a problem, and APIView answer in an APIView too, but how do you send this id from the main page to the controller for page1 , for example? And how to fill in a template based on a JSON response?

    1 answer 1

    To send a dictionary to a template, you need to use a TemplateHTMLRenderer in the renderer_classes parameter, and the template itself is added to the template_name parameter

     from rest_framework.renderers import TemplateHTMLRenderer from rest_framework.response import Response from rest_framework.views import APIView class SomeAPI(APIView): renderer_classes = [TemplateHTMLRenderer] template_name = 'page1.html' def get(self, request, *args, **kwargs): ... return Response({'res':result}) 

    Template documentation

    In general, it is better to separate backend and frontend, configure routing in Angular, and use REST only for sending data.

    • thank. Regarding the routing in Angular - what kind would then be urls.py in REST ? Suppose I transfer /main (by the way, is the main in this case only possible through htaccess configure?), /page1 , etc. in angular, in REST urls.py leave the url for api to execute queries to the database and get a set of records, right? - Emm
    • Yes, separate the frontend and backend. You will only have backends for CRUD with URLs like api/pages/ api/pages/page_id.. , as you described. They are already being accessed from the angular, depending on the page. Go through the tutorial on the site with the documentation for REST, it will become clearer. Here is another good tutorial series godjango.com/41-start-your-api-django-rest-framework-part-1 - Ivan Semochkin
    • About the angular, I correctly understood the idea? app.config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise("/main"); $stateProvider .state('main', { url: "/main", templateUrl: "main.html", controller: function($scope){ $http.get('/someapi/info?type=main').success(function(answer) { $scope.params = ... }) - Emm
    • and for the second page, where the parameter should be .state('page1', { url: "/page1", templateUrl: "page1.html", controller: function($scope){ $http.get('/someapi/info?type=page1&id='+$scope.id).success(function(answer) { $scope.params = ... - Emm
    • @Emm too hard to understand the code. Well, something like that, according to the routing there is a lot of information, it can be implemented in different ways. Go to the codecacademy tutorial on the angular, they propose to solve a similar problem at the very end, take json from the rest of the service. - Ivan Semochkin