Skip to main content

Posts

Showing posts from March 28, 2024

Django URLs

In Django, the urls.py file is where you define the URL patterns for your web application. There are several ways to write the urls.py file, depending on your project’s structure and requirements. Let’s explore some common approaches: Basic URL Patterns : The simplest way is to define URL patterns directly in the urls.py file using the path() function. For example: from django.urls import path from . import views urlpatterns = [ path( 'home/' , views.home, name= 'home' ), path( 'about/' , views.about, name= 'about' ), ] In this example, the URLs /home/ and /about/ will be routed to the corresponding view functions ( home and about ). Include URLs from Other Apps : If you have multiple apps in your Django project, you can include their URL patterns in the project’s main urls.py using the include() function. This keeps the app-specific URLs organized. from django.contrib import admin from django.urls import include, path urlpatte...