How to Change Default Page of a Django Application

Posted on

Introduction

Well, this article is just showing a simple task as a reference. Actually, it is just showing how to change a default page which is normally appear upon executing Django application in the first place. This article will show it by doing a simple change to several script of the Django application. Simple by changing a views.py and also a urls.py file. It is also changing a settings.py file which exist as part of the Django project. The following is the appearance of the default page upon running the Django application :

How to Change Default Page of a Django Application
How to Change Default Page of a Django Application

How to Change Default Page of a Django Application

So, this part will directly show how to achieve it. Changing a default page of a running Django application. Before going on for changing it, there are several requirements in order to that. Just read ‘How to Create a New Project using Django Web-based Framework in Ubuntu Linux via Command Line‘ as a reference for creating Django project.

Moreover, read ‘How to Create a New Application using Django Web-based Framework in Ubuntu Linux via Command Line‘ as a reference for creating Django application. After ensuring Django application exist and running, continue below in order to execute the steps to accomplish it :

  1. The first step is editing the ‘views.py’ file which is part of the Django application. The following is the main structure of the Django application :

    C:\project>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\project
    
    09/06/2022 07:13 AM <DIR> .
    09/06/2022 07:16 AM <DIR> ..
    09/06/2022 07:55 AM <DIR> apps
    08/24/2022 09:30 AM 159,744 db.sqlite3
    08/20/2022 05:17 PM 683 manage.py
    08/20/2022 05:18 PM <DIR> project
    09/06/2022 07:14 AM 126 requirements.txt
    3 File(s) 160,553 bytes
    4 Dir(s) 49,032,695,808 bytes free
    
    C:\project>
    

    So, there is a Django project with the name of ‘project’. Furthermore, there is a Django app with the name of ‘apps’.

  2. Continue on, just get into the ‘apps’ folder. Search for the ‘views.py’ file. Edit the file by adding an index method. Below is the default content of the ‘views.py’ file :

    from django.shortcuts import render
    from django.http import HttpResponse
    # Create your views here.
    

    In other words, just change it into the following content as an example :

    from django.http import HttpResponse
    # Create your views here.
    def index(request):
    return HttpResponse("<h1> Hello World</h1>")
  3. Next, just create an URL for the apps. In order to do that, just create a file with the name of ‘urls.py’ inside the ‘apps’. Just create it so that the following content of the ‘apps’ folder will be exist below :

    C:\project\apps>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\project\apps
    
    09/06/2022 07:55 AM <DIR>        .
    09/06/2022 07:13 AM <DIR>        ..
    08/24/2022 10:02 AM          215 admin.py               
    08/24/2022 08:43 AM          146 apps.py
    08/26/2022 02:19 PM <DIR>        migrations
    08/26/2022 02:05 PM        1,548 models.py
    08/24/2022 08:43 AM           63 tests.py
    09/06/2022 08:03 AM          118 urls.py
    09/06/2022 10:32 AM          258 views.py
    08/24/2022 08:43 AM 0            __init__.py
    7 File(s) 2,348 bytes
    3 Dir(s) 49,026,093,056 bytes free
    
    C:\project\apps>
    
  4. Following after, just edit the file with the name ‘urls.py’ so that it will have the following content :

    from django.urls import path
    from apps import views
    urlpatterns = [
        path('', views.index, name="index"),
    
  5. After that, just edit the ‘urls.py’ of the project. Actually, it exist in the main folder of the project as follows :

    C:\project\project>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\project\project
    
    08/20/2022 05:18 PM <DIR> .
    09/06/2022 07:13 AM <DIR> ..
    08/20/2022 05:17 PM 403 asgi.py
    09/06/2022 07:57 AM 3,577 settings.py
    09/06/2022 10:31 AM 816 urls.py
    08/20/2022 05:17 PM 403 wsgi.py
    08/20/2022 05:17 PM 0 __init__.py
    09/06/2022 10:31 AM <DIR> __pycache__
    5 File(s) 5,199 bytes
    3 Dir(s) 49,027,026,944 bytes free
    
    C:\project\project>
  6. So, edit the ‘urls.py’ file exist in the project folder. Below is the original content of it :

    """model URL Configuration
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/4.1/topics/http/urls/
    Examples: 
    Function views     
        1. Add an import:  from my_app import views     
        2. Add a URL to urlpatterns:  path('', views.home, name='home') 
    Class-based views     
        1. Add an import:  from other_app.views import Home     
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home') 
    Including another URLconf     
        1. Import the include() function: from django.urls import include, path     
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls')) """ 
    from django.contrib import admin 
    from django.urls import path, include 
    urlpatterns = [     
        path('admin/', admin.site.urls), ]
    
  7. Just add a line for including the application’s url which is ‘urls.py’ exist in the ‘apps’ folder. Below, it is the revision of the content :

    """model URL Configuration
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/4.1/topics/http/urls/
    Examples: 
    Function views     
        1. Add an import:  from my_app import views     
        2. Add a URL to urlpatterns:  path('', views.home, name='home') 
    Class-based views     
        1. Add an import:  from other_app.views import Home     
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home') 
    Including another URLconf     
        1. Import the include() function: from django.urls import include, path     
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls')) """ 
    from django.contrib import admin 
    from django.urls import path, include 
    urlpatterns = [     
        path('admin/', admin.site.urls),
        path('', include('apps.urls')), 
    ]
    
  8. Finally after that, just execute the main page once more. The following page will appear :

    How to Change Default Page of a Django Application
    How to Change Default Page of a Django Application

One thought on “How to Change Default Page of a Django Application

Leave a Reply