Part 3 - Templates, Views and URLs

 

Create:

mysite/
    news/
        templates/
            news/
                index.html

 

 

Converting Index to TemplateView and defining the template to be index.html

from django.views.generic import TemplateView

class Index(TemplateView):
    template_name = 'news/index.html'

 

Some text for the template:

<h1>Hello World!</h1>


 

URLs (Routing)
 

To keep everything organized, we create a urls.py file for each app we create.

from django.urls import path
from news.views import *

urlpatterns = [
    path('', Index.as_view(), name="index"),
]

 

Then it's a matter of including each app urls.py file in the main urls.py file.

from django.contrib import admin
from django.urls import path
from django.urls import include

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

]

 

Access through the browser:
http://localhost:8000/
 


Tested with Django 5.2



02 March 2023 | Last Updated: 27 Dec. 2025 | jaimedcsilva

Related
  • Part 1 - Virtual Environment
  • Part 2 - Setting up a Django Project
  • Part 3 - Templates, Views and URLs
  • Part 4 - Static and Media Files
  • Part 5 - Models and Database
  • Part 6 - Connecting everything
  • Part 7 - Publishing to the internet

  • Buy Me a Coffee