You are advised to check corresponding youtube video at the end of this article.
Routing refers to the process of directing incoming HTTP requests to the appropriate view function. Routing allows you to map URLs to the code that will handle the request, allowing you to create dynamic and interactive web applications.
Routing is important in Django because it allows you to create clear and well-organized URLs for your application. Without routing, all requests would be directed to a single view function, which would then need to determine what action to take based on the URL provided. This could quickly become confusing and difficult to maintain as the application grows in size and complexity.
Routing also allows you to pass arguments and parameters to your views, which can be used to further customize the behavior of your application. For example, you might pass a user ID to a view function to display a specific user's profile page.
Go to C:\todo_generic\todo_generic where we need to add our todo app to settings.py
Section that we need to find inside settings.py is INSTALLED_APPS, add todo at the bottom of the list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'todo',
]
Then, inside todo directory create templates dierctory that will hold our html templates.
Inside templates directory create index.html page with a little bit of content for testing purposes. One h1 heading od simple paragraph will be enough.
Now, we need to fix views.py that will hold functions. They will be activated when visitors access specific paths on our website.
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {})
Also, inside todo directory, create routing table urls.py so that paths can trigger functions from views.py
from django.urls import path
from .import views
urlpatterns = [
path('', views.index, name='index'),
]
Next thing, our local urls.py must be included in general urls.py inside todo_generic directory.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('todo.urls'))
]
After we are done with routing, let's start our server, so we can access homepage (index.html) at 127.0.0.1:8000
C:\todo_generic>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
July 27, 2020 - 15:17:56
Django version 3.0.8, using settings 'todo_generic.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[27/Jul/2020 15:18:16] "GET / HTTP/1.1" 200 30
[27/Jul/2020 15:18:19] "GET / HTTP/1.1" 200 30
[27/Jul/2020 15:18:19] "GET / HTTP/1.1" 200 30
[27/Jul/2020 15:18:19] "GET / HTTP/1.1" 200 30
No comments:
Post a Comment