-->

Types of URLS and using named URLS - Django

 

What is URL ?

Everybody say's that URL means "Uniform Resource Locator" . But nobody understands it's meaning. But, now you will know
Uniform: remaining the same in all cases and at all times
Resource: any asset that can be consumable(in our case it's a webpage)
Locator: anything that helps us to find a resource (in our case it's a web-address )
url helps us to find the webpage on internet

 

What is static URL ?

The content in static url cannot change with-respect to time.
Examples:
* Contact us page
* About Us etc.

 

What is dynamic URL ?

The content in static url  changes with-respect to time.
* This kind of urls mostly used in e-commerce kinds of websites.

What is SEO(Search Engine Optimization) ?

Search engines crawls/downloads the contents of webpages through URL's.
It stores our content and url to improve their search results.
Search engine helps web user to find our web contents by indexing urls. 

 

Advantages using static URL ?

search engines indexes static urls more quickly than the dynamic urls.

 

Why we use named urls in Django ?

Many of us use hard coded urls if we do not have idea of named urls.
In some cases we have to change the urls. In this kinds of situations we have to find old url and replace it in all templates/html files.
By using named urls we can avoid it and if we change old url with new-one in urls.py file that will automatically generates the url with new url.

 

How to use named URL's in Django ?

myproject/urls.py
from django.conf.urls import patterns, include, url

from . import views

urlpatterns = [
   url(r'^$', views.home, name = 'home'),
   url(r'^blog/', include('blog.urls', namespace='blog')),
]
blog/urls.py
from django.conf.urls import patterns, include, url

from . import views

urlpatterns = [
   url(r'^post/(?P)/$', views.post, name='post')),
]
we can generate url in views  with "reverse" or "reverse_lazy" functions.
# reverse("namespace:name", kwargs={}) or reverse_lazy("namespace:name", kwargs={})
# reverse("name", kwargs={}) or reverse_lazy("name", kwargs={})
>>> url = reverse("blog:post", kwargs={"id": 8})
>>> print(url)
>>> 'blog/post/9/'
template.html
# {% url 'namespace:name' kwarg1=value kwarg2=value%}
# {% url 'name' kwarg1=value kwarg2=value%}
{% url 'blog:post' id=9 %}
for reference visit:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
https://docs.djangoproject.com/en/1.10/ref/urlresolvers/#django.urls.reverse

Buy a product to Support me