We have AngularJS embedded into our Django application, with URL routing handled by AngularJS ui-router. All is working fine navigating between partials using ui-sref and clicking around within the application.
return $stateProvider.state('root.dashboard', {
abstract: true,
url: 'dashboard/'
}).state('root.dashboard.profile', {
url: 'profile/',
views: {
'@': {
templateUrl: Urls['dashboard:profile'](),
controller: 'ProfileController'
}
}
}).state('root.dashboard.home', {
url: '',
views: {
'@': {
templateUrl: Urls['dashboard:dashboard_home'](),
controller: 'DashboardController'
}
}
...
The problem is when the user has navigated to a non-root page (say for example http://example.com/dashboard/profile/), and the user refreshes the browser, re-loads the browser's URL or simply pastes in the non-root URL directly into the browser. Instead of loading the page retaining the same URL in the browser, the user is getting redirected to the root page (http://example.com/dashboard/) in this case.
Since routing is handled by Angular, on the server side we don't have any url routes defined for those non-root URLs; instead we have middleware that redirects 404s to the root page:
class Redirect404(object):
def process_response(self, request, response):
if response.status_code != 404 or request.method != 'GET':
return response
return HttpResponsePermanentRedirect('/dashboard')
We expect that the router would be able to maintain the original URL and bring the user back to the original page (i.e. 'dashboard/profile'). Note we have set HTML5Mode in Angular as follows:
$locationProvider.html5Mode = true;
There is some mistake in our understanding and/or setup, and would appreciate clarification.