Monday, August 8, 2022
HomeWordPress DevelopmentMaking a URL Shortener service in Python Django

Making a URL Shortener service in Python Django


URL shorteners have turn out to be a preferred service on the internet. Firms like bitly are making nice fortunes from them. However typically while you desire a customized URL you get to pay for the service. So on this tutorial I’m going to point out you the way to construct a URL shortener service in Django.

On this tutorial I anticipate you to be accustomed to templates and types as a result of i wont cowl them however as a substitute i’ll simply present you the way to pragmatically construct create the service in python code.



Setup

To setup the undertaking, we will want just one bundle. django-urlshortner. So lets start by putting in the bundle utilizing pip. This asumes you might have already setup a django undertaking.

pip set up django-urlshortner
Enter fullscreen mode

Exit fullscreen mode

After you must add the urlshortner app to your INSTALLED_APPS in settings.py



Configuration

INSTALLED_APPS = [
    # ....
    "urlshortner"
]
Enter fullscreen mode

Exit fullscreen mode

Then you definitely migrate the fashions to your database

python3 handle.py migrate
Enter fullscreen mode

Exit fullscreen mode

Lastly add the routes to your URLConf in your urls.py of your undertaking.

url_patterns = [
    # ...
    path("r/", include("urlshortner.urls")),
]
Enter fullscreen mode

Exit fullscreen mode

Now you’re good to go.



Utilization

The library gives an inventory of utils to create shortened urls.

To create a brief model of a url use the shorten_url operate from urlshortner.utils module

# python3 handle.py shell
from urlshortner.utils import shorten_url

url_route = shorten_url(
    "https://github.com/jim-junior/django-urlshortner",
    is_permanent=False
)

print(url_route)
# >>> 0ee3f0
Enter fullscreen mode

Exit fullscreen mode

Now you can navigate to you the route that you just assigned to urlshortner.urls in your URLConf add the returned worth att the top of the url. On this case it could be http://localhost:8000/r/0ee3f0/ and this could redirect you to the fitting URL

Generally you need to create a customized URL. For instance you need to create a brief hyperlink for a weblog about your new product and also you desire a url that’s straightforward to recollect. You possibly can add this simply by including the worth argument to the shorten_url operate

from urlshortner.utils import shorten_url

url_route = shorten_url(
    "https://myblog.com/weblog/2022/10/10/..../my-new-product",
    worth="NewProduct"
    is_permanent=False
)
Enter fullscreen mode

Exit fullscreen mode

Now you can navigate to https://localhost/r/NewProduct and It should redirect you

Now that you know the way to make use of the bundle i feel you possibly can intergrate it with you undertaking.

Hope this text was useful. You possibly can git the undertaking a star on Github or in case you have any thought so as to add on you possibly can contribute to its repository. And bythaway I’m the Creator of this library.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments