Create New Page
In this guide we will see how we can create a new page and render it through a path.
Step 1:
Using our html structure first create an html page in templates folder you want to
render.
Note: Remember that even though we added a new app, Django will not recognize it until it is added to the INSTALLED_APPS
Step 2:
Configure your app in settings.py. For that you have to follow below code.
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'admiroapp',
]
Step 3:
Next we need to update django_project/settings.py to tell Django the location of our
new templates directory. This is a one-line change to the setting "DIRS" under TEMPLATES.
# django_project/settings.py
TEMPLATES = [
{
...
"DIRS": [BASE_DIR / "templates"], # new
...
},
]
Step 4:
Now,we update the django_project/urls.py
file to point at our pages app
and then within pages we match views to URL routes. Also we need to import include dependencies.
from django.contrib import admin
from django.urls import path, include # new
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("admiroapp.urls")), # new
]
Step 4:
Now,we have to create a new urls.py file in our django_app/url.py.Here we will create
new path to render our page.As shown below,we will provide path as the first parameter, and provide function
which is returning the templates as the second parameter. This function is present in views.py.As the third
parameter give a unique name for our newly created path.
# django_app/urls.py
from django.urls import path
from .views import
urlpatterns = [
path("", views.index, name="index"),
]
Step 5:
In final step we have to define our urls.py path in views.py.This views.py file takes a
web request and returns a web response.
# django_app/views.py
def index(request):
return render(request,'index.html')