Django URLs tutorial step by step
In this post, we learn all about URLs or routes in Django step by step. we know whenever user request for something we can handle using URLs. so set URLs for that user what we want to show a text message or a view using URL request in Django framework.
Open your urls.py
When you open urls.py by default you will find one URL.
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Step: 1 Create URL for about request URLs.py
Here we write two lines of code first one is for import views and the second one is for URLs handle here I create a URL for hande a request for about.
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('about/', views.about, name=about),
]
Step: 2 Add new file views.py to handle URL request
Create a new file views.py and add some to code to response request.
from django.shortcuts import render
def about(request):
return render(request, 'index.html')
Step: 3 create a new view HTML file
The best practice is before creating a view first create a directory named templates than create an HTML file named index.html.
Now we have an about page add some code to view.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=UTF-8>
<title>Title</title>
</head>
<body>
<h1>I am about page</h1>
</body>
</html>
Step: 4 Update setting.py file for using templates
Here we going to tell python we are going to use a template file which is in templates folder so use this.
Here just update 'DIRS' with folder name.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Step: 5 run you python server to check your application
So here we can see the result now we successfully completed tutorials on Django URLs tutorial step by step hope its help you.
Best of Luck
Brijpal Sharma
Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...
0 Comments
You must be logged in to post a comment.