Wednesday, October 14, 2020

Creating Home Page

After creating the main Django application called "project" in Django term, we could also create a number of separated applications and link them to the main application for them to run also when the the main application starts. For example, to create a separated application for the home page, we need to write a statement as below:

python manage.py startapp home

Next, we need to create the first controller or view in Django term in order to display the home page of our application.

#home/views.py
from django.http import HttpResponse

def index(request):
  return HttpResponse('Hello World!')

To run the code in the module views.py above, we need to create a route or URLconfig to this module as below:

#home/urls.py
from django.urls import path

from . import views

urlpatterns = [
  path('', views.index, name='index'),
]

Finally, to run the code in the module home/urls.py, we have to include this file in the module mysite/urls.py as below:

#mysite/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
  path('', include('home.urls')),
  path('admin/', admin.site.urls),
]

GitHub: https://github.com/Sokhavuth/django
Heroku: https://khmerweb-django.herokuapp.com/

No comments:

Post a Comment