Answer the question
In order to leave comments, you need to log in
How to set up routing in Angular -route + Django?
Good afternoon! I'm making a single page application on Django + Angular using Rest-framework.
At the stage of setting up routing using angular-route, the following problem arose: when opened in a browser, an empty page is screened, while errors continue to fly out in the developer console, and the loading icon constantly spins on the tab. Routing is configured according to the example from elementary tutorials:
app.js
var app = angular.module('uniApp', ['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
controller : 'mainCtrl',
templateUrl : '../views/main.html'
})
.when('/\d', {
controller : 'detailCtrl',
templateUrl : '../views/detail.html'
})
.otherwise({
redirectTo:'/'
});
$locationProvider.html5Mode(true);
})
app.controller('mainCtrl', function ($scope, $http) {
$http.get('http://127.0.0.1:3000/api/v0/cases/').success(function(data){
$scope.cases = data;
});
});
app.controller('detailCtrl', function($scope) {
});
<!DOCTYPE html>
<html ng-app="uniApp">
<head>
<meta charset="utf-8">
<base href="/">
<title>UNI DIGIT</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-route.min.js"></script>
{% load staticfiles %}
<script type="text/javascript" src="{% static 'js/app.js' %}"></script>
</head>
<body ng-controller="mainCtrl">
<div ng-view></div>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Take a look in the developer's console to see what addresses go to requests for the templates specified in templateUrl. It seems to me that Dzhanga instead gives the index page, which initializes the angular, looks for the route, tries to load the template, gets the main one instead, and so on until it stops.
If I guessed correctly, what are the solutions:
1. Wrong: put all angular templates in a directory for static files (see staticfiles )
2. Do not use Django for the frontend at all. As a backend with an API, it is great, but for SPA and, in particular, for Angular, it is not very good. I would advise developing SPA in a separate repository that knows nothing about the backend.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question