Creating a URL Shortener using Flask and Python

Posted by

URL Shortener in Flask with Python

Introduction to URL Shortener in Flask with Python

URL Shorteners are tools that take long URLs and convert them into shorter, more manageable links. These shortened URLs are easier to share and can be customized to create memorable links for marketing purposes.

In this article, we will discuss how to create a URL shortener using Flask, a popular web framework in Python.

Setting up Flask

First, make sure you have Python installed on your computer. You can then install Flask using pip, the package manager for Python.

pip install Flask

Once Flask is installed, you can create a new Flask application. Here’s a simple example to get you started:


from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
return 'Hello, World!'

if __name__ == '__main__':
app.run()

Save the above code in a file called app.py and run it using the command python app.py. You should see a message that the Flask application is running on a local server.

Creating the URL Shortener

Now that we have Flask set up, we can start building our URL shortener. We’ll need a form for users to enter the long URL and a function to generate the shortened URL.


from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/shorten', methods=['POST'])
def shorten_url():
long_url = request.form['long_url']
# Logic to generate shortened URL
return 'Shortened URL'

In the code above, we have created two routes – one for displaying the form and one for processing the form data. We will use a simple algorithm to generate the shortened URL, such as hashing the long URL or using a random string.

Displaying the Shortened URL

Finally, we need to display the shortened URL to the user. We can modify the shorten_url() function to redirect the user to the shortened URL or display it on a new page.


from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/shorten', methods=['POST'])
def shorten_url():
long_url = request.form['long_url']
# Logic to generate shortened URL
shortened_url = 'Shortened URL'
return render_template('shortened.html', shortened_url=shortened_url)

We can create a new HTML file called index.html to display the form and a file called shortened.html to display the shortened URL.

Conclusion

In this article, we have learned how to create a simple URL shortener using Flask with Python. With some additional features and security measures, you can enhance the functionality of the URL shortener and deploy it to a live server.

Thank you for reading and happy coding!

0 0 votes
Article Rating
14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@user-li4vi8xf8p
10 months ago

hii

@BestURLShortenerBioPageQRCode
10 months ago

Thank you for sharing this much of information.

@omarabdul-hafiz6467
10 months ago

Amazing! Could you please make a video about how to use stuff like Flask-Admin and how to limit access to certain parts of your app/website to admins and registered users?

I would be really, really thankful! 🙂

@muckiSG
10 months ago

Is the source code to this episode on the github server? I am not able to find it…

@HoracioSantisteban
10 months ago

Best URL shortener video on YouTube! Slightly advanced for my level but easy to follow, great learnings!

@gitit.
10 months ago

How do I deploy this?

@13antonispao
10 months ago

Thanks for the interesting projects and for including all the mistakes, it's a good learning experience. A real shame about all the annoying spam in the comments, I make sure to report them on each video.

@gu00col
10 months ago

hey brother, try use the new feature of youtube to dub automatic audio from your videos, can be helpful to not english native speaker. you have a very god content but some times is hard to understand.

@tcgvsocg1458
10 months ago

don t show when you fail..its still good video

@paulthomas1052
10 months ago

Thanks really useful code

@RBAhmet
10 months ago

Why don't you give PapayaHub a shot?

@pablo20237
10 months ago

Very excellent, thank you sir

@Luc1an_
10 months ago

How do we redirect this to the original url. Make a complete project on this please

@Hazza0001
10 months ago

Another great project!