4

I'm using an AngularJS app with Django Rest Framework. However, I've setup my project such that the Angular page is served from within the templates folder of my setup such that

url(r'^$', index)

def index(request):
    return render(request, 'index.html', {})

I'm running the angular app on / url of Django and have setup my ngRoutes as follows:

var sgApp = angular.module('sgApp', [
        'ngRoute',
    ]);

sgApp.config(['$routeProvider','$locationProvider',
    function($routeProvider, $locationProvider){
        $routeProvider.when('/',{
            templateUrl: '/static/partials/index.html'
        });
        $locationProvider.html5Mode(true);
    }
]);

However, when I try to access an invalid url such as localhost:8000/some-random-url, it'll redirect to Django's inbuilt 404 page. Is there a way for me to fix this? Or do I have to completely abstract the angular app from the rest framework?

2 Answers 2

4

You need to add in your angularjs router, the default route value (otherwhise)

sgApp.config(['$routeProvider','$locationProvider',
    function($routeProvider, $locationProvider){
        $routeProvider.when('/',{
            templateUrl: '/static/partials/index.html'
        })
        .otherwise({
            redirectTo: '/'
         });

        $locationProvider.html5Mode(true);
    }
]);

Then add this to the bottom of your urls.py

url(r'^.*$', index),

This means that if requested url did not match any previous url rules, redirect to index. Notice * in ^.*$

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

6 Comments

Hey, how do I make API calls work now that html5mode is present? Whenever I try to make any api calls, it returns to the '/' url present in the $routeProvider
@Newtt Are u making those api calls using $http ? or just broswer?
@Newtt you need to place url(r'^.*$', index) at bottom of your urls.py.It should be the last rule in your urls.py.
Oh yeah! I ended up placing another url after that. Thanks a lot! :)
how about if you want it to stay on the given url? for example the url visited was /orders what should I do so if the user refreshes it the page stays at /orders and not gets redirected back to the index? @levi
|
1

With Django/ DRF, for a series of routes have the index route with the .*$ as the last:

urlpatterns = patterns(
    '',
    url(r'^api/v1/', include(router.urls)),
    url(r'^api/v1/', include('agents.urls')),
    url(r'^api/v1/', include('utils.urls')),
    url(r'^api/v1/', include('authentication.urls')),
    url(r'^api/v1/', include('products.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^accounts/login', LoginView.as_view(), name='login'),
    url(r'^.*$', IndexView.as_view(), name='index'),
)
handler404 = 'views.handle_page_not_found_404'

where index is the last route.

Comments

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.