Django URLs
Django URLs
A Url is a website’s address. Url makes every page available on the internet different from the others. So, without wasting time let's see how Django URLs work
Project Url file
Now, open your Projects urls.py file. After opening the urls.py file you will see a similar interface which has been shown below.
from Django.contrib import admin
from Django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
As you see, there is already a path existing in the URL pattern which is the admin path. Whenever you use /admin after your website URL then your website's admin panel will open for you.
Now, we have to add a blank path in URL patterns. For this import “include” from urls.py use the following command.
from Django.contrib import admin
from Django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('YourAppName.urls')),
]
The above code indicates that whenever the user hits the blank path Django will redirect everything to the app name.URLs and look for further instructions.
App Url file
As we have already mentioned you have to create a urls.py file in your Django app. After creating a Django file you have to import the following things which are given below. And, you also have to provide a path, view, and name for your patterns.
from Django.contrib import admin
from Django.urls import path
from demo import views
urlpatterns = [
path("", views.index, name='home')
]
Now, our Django views and URLs are connected with each other. To check if they are working perfectly or not hit the given path in the browser.
Perfectly working!