1

I have starting building my application in angularJS and django, and after creating a login page, I am trying to redirect my application to a new url after successful login. I am using $location variable to redirect my page. Here is my code:

$scope.login = function() {
    $http({
        method: 'POST',
        data: {
            username: $scope.username,
            password: $scope.password
        },
        url: '/pos/login_authentication/'
    }).then(function successCallback(response) {
        user = response.data
        console.log(response.data)
        if (user.is_active) {
            $location.url("dashboard")
        }
    }, function errorCallback(response) {
        console.log('errorCallback')
    });
}

My initial url was http://localhost:8000/pos/, and after hitting the log in button, the above function calls, and I am redirected to http://localhost:8000/pos/#/dashboard. But I am unable to catch this url in my regex pattern in urls.py file:

My project urls.py file:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^pos/', include('pos.urls')),
    url(r'^admin/', admin.site.urls),
]

And my pos application's urls.py file:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^login_authentication/$', views.login_authentication, name='login_authentication'),
    url(r'^#/dashboard/$', views.dashboard, name='dashboard')
]

Using this, I am getting the same login page on visiting this http://localhost:8000/pos/#/dashboard link. This means that in my urls.py file of my pos application, it is mapping my http://localhost:8000/pos/#/dashboard to first object of urlpatterns:url(r'^$', views.index, name='index'). How do I make python differentiate between both the links?

psr
  • 2,619
  • 4
  • 32
  • 57
  • That sort of page access is pretty typical of Angular. You'll want to look at how to have Angular request a specific URL from the Django side when that URL is requested. – cardonator Jul 21 '16 at 19:05

2 Answers2

1

You have some major misunderstanding about and anchor in url. The anchor is called officially Fragment identifier, it's not part of the main url, so if you have # when you visit an url like http://localhost:8000/pos/#/dashboard, your browser would treat the remaining #/dashboard as the anchor in page that http://localhost:8000/pos/ renders. You shouldn't be even using it in your urls.py definition. Please read the link above more carefully about the usage of an anchor.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • I got it. What approach and changes should i make in my login function so that I can redirect my page to a link such as `http://localhost:8000/pos/dashboard`? – psr Jul 21 '16 at 19:14
  • Check this SO answer on how to do the redirection after authentication: http://stackoverflow.com/questions/4870619/django-after-login-redirect-user-to-his-custom-page-mysite-com-username#answer-7061358. Note that I'm using an anchor for this link :) – Shang Wang Jul 21 '16 at 19:30
  • Noticed it! Thanks! :) – psr Jul 21 '16 at 20:10
  • Although the post that you mentioned was helpful, but the method that they mentioned was returning new url in `response.data` in the successCallBack but not redirecting to a new url. – psr Jul 23 '16 at 03:25
  • Are you sure we are looking at the same thing? I don't even know what are you talking about, like `response.data` and `successCallBack`, where are they defined? – Shang Wang Jul 23 '16 at 19:03
0

Using help from this answer, I figured a good redirection method through angular which doesn't append any anchor tag using $window:

$scope.login = function() {
    $http({
      method: 'POST',
      data: {
        username: $scope.username,
        password: $scope.password
      },
      url: '/pos/login_authentication/'
    }).then(function successCallback(response) {
        user = response.data
        console.log(response.data)
        if (user.is_active) {
            $window.location.href = '/pos/dashboard';
        }
    }, function errorCallback(response) {
        console.log('errorCallback')
      });
}
Community
  • 1
  • 1
psr
  • 2,619
  • 4
  • 32
  • 57