create your first Django view and URL I remember finally getting my first custom page to show up in the browser — not the default Django welcome page, but something I actually typed myself. It was just the words “Hello, world” on a blank white page, and I still felt genuinely proud of it. If you’ve been reading through my Django series and understand what views and URLs do conceptually, this post is where we stop talking about it and actually build one.
Grab your project from the earlier posts, or start a fresh one — either works.
Table of Contents
Step 1: Make Sure Your App Is Ready (Create Your First Django View and URL in 6 Simple Steps)
Before creating your first view and URL, you need an app to hold them. If you haven’t already created one, run this from your project’s root folder:
python manage.py startapp pages
Then open settings.py and add 'pages' to your INSTALLED_APPS list. If you skip this step, Django won’t recognize the app exists, and nothing you build next will show up.
Step 2: Open views.py and Write Your First View
Inside the pages app, open views.py. Delete anything already there and add this:
python
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, world! This is my first Django view.")
That’s the entire view. A function that takes a request and returns a response. I know it looks almost too simple, but this is genuinely the foundation of every page you’ll ever build in Django, no matter how complex it eventually gets.
Step 3: Create a urls.py Inside Your App
By default, Django doesn’t create a urls.py file inside a new app — you have to add it yourself. Inside the pages folder, create a new file called urls.py and add:
python
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
This connects an empty path (your homepage) to the home function you just wrote in views.py.
Step 4: Connect Your App’s URLs to the Project
Now open the project-level urls.py — the one sitting next to settings.py, not inside your app. Update it to include your app’s URLs:
python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
This line tells Django: “any request that isn’t /admin/, hand it over to the pages app to figure out.”
Step 5: Run the Server and Check It
Time to see it working. Run:
python manage.py runserver
Open your browser and go to http://127.0.0.1:8000/. Instead of Django’s default rocket page, you should now see your own message: “Hello, world! This is my first Django view.”
If you see that, you’ve officially created your first working view and URL from scratch.
Step 6: Add a Second Page to Understand Routing
One view is a good start, but the real value of URLs and Views shows up once you add a second page. Back in views.py, add:
python
def about(request):
return HttpResponse("This is the about page.")
Then update your app’s urls.py:
python
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
Visit http://127.0.0.1:8000/about/ and you’ll see your second page load, completely separate from the homepage. This is the exact pattern you’ll repeat for every new page on a real Django site — write a function in views.py, connect it to a path in urls.py.
What You Just Actually Built
Walking back through it, here’s what happened: you defined logic in a view, mapped a URL to that logic, and connected the app’s routing into the project’s main routing. That’s the same chain I covered conceptually in my earlier post on how Django URLs and Views work — except now you’ve built it yourself instead of just reading about it.
A Small Improvement Worth Trying
Returning plain text with HttpResponse works for learning, but real pages use HTML templates instead. If you want to push a little further, try this small upgrade:
python
from django.shortcuts import render
def home(request):
return render(request, 'pages/home.html')
You’d need to create a templates/pages/home.html file with basic HTML inside. I won’t go deep into templates here since that deserves its own post, but it’s a natural next step once views and URLs feel comfortable.
Common Mistakes to Watch For
- Forgetting to
include()the app’surls.pyin the project-level file — the most common one, by far - Typing the function name differently in
views.pyandurls.py - Forgetting to add the app to
INSTALLED_APPS - Missing the trailing slash on a path, causing an unexpected 404
I hit every single one of these when I was learning, sometimes more than once.
Where to Go Next
If any part of this felt unclear, it’s worth going back to my earlier post explaining how Django URLs and Views work conceptually — this tutorial builds directly on top of that explanation. For deeper technical detail on views specifically, the official Django documentation on views is a solid next read.
Final Thoughts
Creating your first view and URL is a small moment, but it’s the one where Django stops feeling theoretical. You wrote a function, connected it to an address, and watched a browser display exactly what you told it to. Everything more advanced you build later is just a bigger version of this same loop.
Go add a third page right now, on your own, without looking back at this post. That’s the fastest way to know it’s actually sunk in.
Django View URL එකක් හදන විදිය සිංහලෙන් – පියවරෙන් පියවර
මට හොඳටම මතකයි, මගේ first custom page එක browser එකේ පේන්න ගත්ත moment එක – default Django welcome page එක නෙවෙයි, ඇත්තටම මම type කරපු දෙයක්. ඒක just “Hello, world” කියන වචන ටික විතරයි white page එකක, ඒත් මට ඇත්තටම proud feel වුණා. ඔයා මගේ Django series එක කියවලා, views සහ urls conceptually තේරෙනවා නම්, මේ post එකෙන් අපි කතා කරන එක නවත්තලා ඇත්තටම එකක් හදමු.
කලින් post වල project එකම use කරන්න, නැත්නම් අලුත් එකක් start කරන්න – දෙකම වැඩේ.
Step 1: ඔයාගේ App එක Ready ද කියලා බලන්න
First view සහ URL එක හදන්න කලින්, ඒවා තියාගන්න app එකක් ඕන. කලින් app එකක් හදලා නැත්නම්, project root folder එකෙන් මේක run කරන්න:
python manage.py startapp pages
ඊට පස්සේ settings.py file එක open කරලා, INSTALLED_APPS list එකට 'pages' add කරන්න. මේ step එක miss කළොත්, Django app එක exist වෙනවා කියලා recognize කරන්නේ නෑ, ඊළඟට හදන කිසිම දෙයක් load වෙන්නේ නෑ.
Step 2: views.py Open කරලා First View එක ලියන්න
pages app එක ඇතුළේ, views.py file එක open කරන්න. දැනටමත් තියෙන දේවල් delete කරලා, මේක add කරන්න:
python
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, world! This is my first Django view.")
ඒක තමයි view එකේ ඔක්කොම. Request එකක් ගන්නා, response එකක් return කරන function එකක්. මට තේරෙනවා ඒක too simple කියලා පේනවා, ඒත් ඇත්තටම ඔයා පස්සේ කොච්චර complex වුණත් හදන හැම Django page එකකම foundation එක ඒකයි.
Step 3: App එක ඇතුළේ urls.py එකක් Create කරන්න
Default විදිහට, Django අලුත් app එකක් ඇතුළේ urls.py file එකක් create කරන්නේ නෑ – ඔයාම ඒක add කරගන්න ඕන. pages folder එක ඇතුළේ, urls.py කියලා file එකක් අලුතින් create කරලා මේක add කරන්න:
python
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
මේකෙන් empty path එකක් (ඔයාගේ homepage එක) views.py වල ලියපු home function එකට connect කරනවා.
Step 4: App එකේ URLs Project එකට Connect කරන්න
දැන් project-level urls.py file එක open කරන්න – settings.py එක ළඟ තියෙන එක, app එක ඇතුළේ නෙවෙයි. ඔයාගේ app එකේ URLs include කරන්න update කරන්න:
python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
මේ line එකෙන් Django ට කියනවා: “/admin/ නොවන හැම request එකක්ම, pages app එකට දීලා handle කරගන්න දෙන්න.”
Step 5: Server එක Run කරලා Check කරන්න
දැන් ඒක වැඩ කරනවද බලමු. මේක run කරන්න:
python manage.py runserver
Browser එක open කරලා http://127.0.0.1:8000/ එකට යන්න. Django ගේ default rocket page එක වෙනුවට, දැන් ඔයාගේම message එක පේන්න ඕන: “Hello, world! This is my first Django view.”
ඒක පේනවා නම්, ඔයා officially ඔයාගේ first working view සහ URL එක scratch එකෙන් create කරලා ඉවරයි.
Step 6: Routing තේරෙන්න Second Page එකක් Add කරන්න
View එකක් good start එකක්, ඒත් URLs සහ Views වල real value එක පේන්නේ second page එකක් add කරාට පස්සේ. ආපහු views.py වලට, මේක add කරන්න:
python
def about(request):
return HttpResponse("This is the about page.")
ඊට පස්සේ ඔයාගේ app එකේ urls.py update කරන්න:
python
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
http://127.0.0.1:8000/about/ එකට visit කරන්න, ඔයාට second page එක, homepage එකෙන් සම්පූර්ණයෙන්ම separate විදිහට load වෙනවා පේනවා. Real Django site එකක අලුත් page එක එකකටම repeat කරන exact pattern එක ඒකයි – views.py වල function එකක් ලියන්න, urls.py වල path එකකට connect කරන්න.
ඇත්තටම ඔයා දැන් හදලා ඉවර
ආපහු මුලට ගිහින් බැලුවොත්, වුණේ මේකයි: view එකක logic එකක් define කළා, ඒ logic එකට URL එකක් map කළා, app එකේ routing එක project එකේ main routing එකට connect කළා. මම කලින් post එකේ conceptually cover කරපු Django URLs සහ Views වැඩ කරන chain එකම තමයි මේක – ඒත් දැන් ඔයා ඒක actual විදිහට හදලා, ඒ ගැන කියවලා විතරක් නෙවෙයි.
Try කරන්න වටින Small Improvement එකක්
HttpResponse එකෙන් plain text return කරන එක learning එකට හොඳයි, ඒත් real pages HTML templates use කරනවා. ටිකක් ඉස්සරහට යන්න ඕන නම්, මේ small upgrade එක try කරන්න:
python
from django.shortcuts import render
def home(request):
return render(request, 'pages/home.html')
මේකට templates/pages/home.html file එකක් basic HTML එකක් එක්ක create කරන්න ඕන. Templates ගැන deep කතා කරන්නේ නෑ මෙතන, ඒකට වෙනම post එකක් ඕන, ඒත් views සහ urls comfortable feel වුණාට පස්සේ ඒක natural next step එකක්.
බලන්න ඕන Common Mistakes
- App එකේ
urls.pyඑක project-level file එකටinclude()කරන්න අමතක වීම – මේක ගොඩක් common views.pyසහurls.pyවල function name එක වෙනස්ව type කිරීම- App එක
INSTALLED_APPSවලට add කරන්න අමතක වීම - Path එකේ trailing slash එක miss කිරීම, ඒකෙන් unexpected 404 එකක් ඒම
මම ඉගෙන ගන්නකොට මේ හැම mistake එකක්ම කළා, සමහර වෙලාවට එකකට වඩා.
ඊළඟට කරන්න ඕන දේ
මේකෙන් කොටසක් clear නැත්නම්, Django URLs සහ Views conceptually explain කරපු මගේ කලින් post එකට ආපහු යන එක වටිනවා – මේ tutorial එක build වෙන්නේ ඒ explanation එක උඩම. Views ගැන deeper technical detail ඕන නම්, official Django documentation on views එක හොඳ next read එකක්.
අවසාන කතාව
First view සහ URL එක create කිරීම කුඩා moment එකක්, ඒත් ඒක තමයි Django theoretical feel වීම නවත්තලා practical feel වෙන්න පටන් ගන්න තැන. ඔයා function එකක් ලිව්වා, ඒක address එකකට connect කළා, browser එකෙන් ඔයා කියපු දේම display කරනවා බැලුවා. පස්සේ ඔයා හදන advanced ඕනෑම දෙයක්, මේ same loop එකේම ලොකු version එකක් විතරයි.
දැන්ම, මේ post එකට ආපහු බලන්නේ නැතුව, ඔයාගේම third page එකක් add කරලා බලන්න.
