Django URL Mapping

Django URL Mapping In Hindi – Django In Hindi

Python Django

Django URL Mapping In HindiDjango In HindiIs Post Me Django URL Mapping Ko Cover Karne Wale Hai Or Django URL Mapping Kiya Hoti Hai Ye Bhi Dekhne Wale Hai Or Is URL Mapping Ka Kiya Istemal Hota Hai |

Previous – Views.py

Django URL Mapping In Hindi

Aap Ye To Pata Hi Hoga Ki, Kisi Bhi Website Bina URL ke Nhi Hoti Hai, Jaise Ki Aap Maan ke Chalo Ki MasterPrograming.com Bhi Ek URL Hi Hai, Or MasterPrograming.com/python Bhi Ek URL Hai, Lekin Sawal Ye Hai, Ki Ye URL Kaise Banate Hai, Aaj Ki Isi Post Me URL Mapping Ke Bare Me Dekhne Wale Hai |

Django URL Mapping Karne Ke Liye Aapko myproject Folder Me Ek urls.py File Mil Jayegi, Jo Ye File URLS Files Ki Main File Hoti hai, Mera Kahne Ka Matlab Hai, Ki jab Bhi Hamari Website Ka Koi Bhi URL Web Browser Me Hit Yani Request Send Karega Server Ko To, Sabse Pahle myproject Folder Ki urls.py file Run Hogi Or Ye Check karegi Ki URL Agar Hai, To Use Kaha Bhejna Hai. Ise Ham Kisi Ek Simple Example Se Understand Karte hai |

default myproject/urls.py

"""myproject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

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

Aap Upper Di Gayi myproject/urls.py File Ko Dekh Sakte Hai, Ye Django Default Me Code Provide karta Hai |

Aap Is File me Line 1 se Lekar Line 15 Tak Comments Ko Read Kar Sakte hai, Or Uske Bad 2 import startment Hai, Jo line 16 Par admin ko Import Kiya Gaya Hai, Or Line 17 par path, Yani Ki URL Ko Create karne ke Liye |

Jab Bhi Django me URL Define Karna hota Hai, To Hame urlpatterns = [ ] Yani Ki Python List Ke Under path() Function ka Istemal karke URL Define Kar Sakte Hai.

Aap Line 20 Me Dekh Sakte Hai, path(‘admin/’, admin.site.urls), Likha Hua Hai, Iska Matlab Hai, Ki Agar Domain Name Ke Bad admin (masterprograming.com/admin) type Kiya Jata Hai, To admin.site.urls Me Use Redirect Kar Dega. Maine Aapko Pahle Hi Bataya Hai, Django Admin Default Me Provide karta Hai, isliye Liye Hame Koi Bhi Coding Nhi karni Hoti Hai.

Ab Ham Chahte Hai, Ki Jab user Website Ko Open kare with default url Ke Sath (masterprograming.com), To Wo Usko Jo templates/myapp/hello.html Ke Under Data Ko Show Karde, To Chaliye Start karte hai |

To Sabse Pahle Hame Ek Home URL Create Karna Hai myproject/urls.py file ke Under Joki Wo Url Pahle myapp/urls.py file Ko Call Kare |

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
    path('home/',include('myapp.urls')),
]

path(‘ ‘), path(‘home/’) -> domain.com ya fir domain.com/home -> Ko Hamne Rakha Hai, Kyoki Ham Chahte Hai, ki Jab Bhi Domain Open Hoto Wo To Main Domain URL ko Hit Kare And path(”, include(‘myapp.urls’)), Ya Fir Hame Kaha ki Jab Bhi Blank Domain Open Hoto Wo Include Karde myapp.urls File Ko yani Ki myapp Folder ke Under urls.py File Ko |

Agar Ab Tak Dekhe To, Hame Ek URL Create Kar Liya Hai, Jo Domain Open Hone Par myapp.urls File Ko Call Karega |

Ab Ham myapp/urls.py File Create Karte Hai, Dhiyan Rahe Jab Bhi ham Django Me app Create karte hai, To usme urls.py File Ko Create Karna Hota Hai |

Create – myapp/urls.py

Ab Ham myapp/urls.py Me Code karenge Ki, Agar myproject/urls.py Se Kuch Include Kiya Gaya hai, To Usme Hame Kiya Dikhana Hai |

from django.urls import path 

urlpatterns = [
    path('home', views.myapp , name= "home"),
    path('' , views.myapp , name='home'),
    ]

Jab Bhi myproject/urls.py File Se myapp/urls.py File Ko Call Kiya Jayega To myapp/urls.py me Run Hogi, Or Aap Dekh Sakte Hai, Jab Bhi domain name se Website Open Hogi To Pahle myproject/urls.py Run Hogi Or Uske Bad myapp/urls.py Run Hogi |

Hamne myapp/urls.py File me Code Kiya Hai, Ki Jab Bhi domain.com/home ya domain.com Link Hit Hoga Browser Me To myapp views.myapp File Ko Run Kardo Or Uske Bad name=”home” Functions Ko Run Kardo |

  • views.myapp – Jab Bhi path(‘ ‘) Uske url Ko Hit Kiya Jega To views.myapp Par Jeyaga Matlab current folder ki views file par |
  • name =”home” – Yaha Us views Me Is Name se Functions Hoga, Aap Function Ka Kuch Bhi name Rakh Sakte Hai |

Ab Hame myapp/views.py File Me home name se Function Ko Create Karna Hai Or Fir Ham Kuch Text Ke Sath Templates Ko Return Karenge |

from django.shortcuts import render

def home(request):
	name = "Danish Ali"
	return render(request, "myapp/home.html",{"name" : name})

Aap Dekh Sakte Hai, Hamne ek home function ke Under name variable liya Hai or fir us Variable ko render function se return kar diya hai Template me |

Ab Ham template/myapp/home.html file me Kuch HTML Code Likhte Hai, Or Jo name Variable Se Data Aaya Hai, Use Isme Show Karate Hai.

<!DOCTYPE html>
<html>
<head>
	<title>Django Tutorial</title>
</head>
<body>
	<h1> Your Name Is : {{name}} </h1>
</body>
</html>

Ye Sab Karne Ke Bad Aap Niche Diye Cmd Line Ko Likhar Enter Press Karna Hai |

$ python manage.py runserver

Isko Run Karne Ke Bad Aap Django Ka Server Istemal Kar Sakte Hai, Django Khud Ka Server Provide Karta Hai |

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *