How to Solve Cannot Load Django Application when using Additional URL or ProxyPass Access

Posted on

Introduction

In this article, there will be a description of a condition which is explaining the situation as it exist in the title of this article. Basically, upon setting the URL address of a Django application by adding an additional URL address on the back of the original URL will end in an access failure. For an example, a simple URL address for the Django application can be as in ‘http://www.mydjangosites.com’. On the other hand, for a specific request, the URL address for accessing the Django application can be in other different kind of form. In this context for an example, it is ‘http://www.mydjangosites.com/myapp’. That additional URL address part ‘/myapp’ will cause the Django-based application cannot display the page properly. It is because it cannot find the ‘/myapp’ path in any definition part of the URL normally in ‘urls.py’ of the project. The following is the image describing the condition :

How to Solve Cannot Load Django Application when using Additional URL or ProxyPass Access

Solution

In order to solve the problem, there is a trick as a solution for it. The solution in this context is very simple. The following is the step for implementing the solution :

  1. First of all, just open the file which is defining the URL address of the project. In this context, if there is a project with the name ‘myproject’. Furthermore, in order to define the URL address, there is a file exist with the name of ‘urls.py’.

  2. In the ‘urls.py’ file, just modify the definition of the URL address. The following is the actual content of the original ‘urls.py’ file :

    # -*- encoding: utf-8 -*-
    """
    Copyright (c) 2019 - present AppSeed.us
    """
    from django.contrib import admin
    from django.urls import path, include  # add this
    urlpatterns = [
        path('admin/', admin.site.urls),          # Django admin route
        path('', include("apps.authentication.urls")), # Auth routes - login / register
        path('', include("apps.app.urls")),             # UI Kits Html files
        path('ckeditor/', include('ckeditor_uploader.urls')),
        path('', views.index, name='home'),
    ]
    
  3. The modification for the ‘urls.py’ in order to solve the problem exist as follows :

    # -*- encoding: utf-8 -*-
    """
    Copyright (c) 2019 - present AppSeed.us
    """
    from django.contrib import admin
    from django.urls import path, include  # add this
    urlpatterns = [
        path('myapp/', include([
            path('admin/', admin.site.urls),          # Django admin route
            path('', include("apps.authentication.urls")), # Auth routes - login / register
            path('', include("apps.app.urls")),             # UI Kits Html files
            path('ckeditor/', include('ckeditor_uploader.urls')),
            path('', views.index, name='home'),
        ])),
    ]
    

    Just add the following above line :

        path('myapp/', include([
        ...
        ])),
    

    The idea of the above modification to the ‘urls.py’ is to have ‘myapp’ URL path in front of any URL available in the Django-based application. Since it is using a ProxyPass ‘/myapp’, the application will need to use another additional path. So, the efficient way to do that is to be able to modify the ‘urls.py’ by adding the path in front of it.

Leave a Reply