My python code is stated below:
import random
import string
from flask import Flask, render_template, redirect, request
app = Flask(__name__)
shortened_urls = {}
def generate_short_url(length=6):
chars = string.ascii_letters + string.digits
short_url = "".join(random.choice(chars) for _ in range(length))
return short_url
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
long_url = request.form['long_url']
short_url = generate_short_url()
while short_url in shortened_urls:
short_url = generate_short_url()
shortened_urls[short_url] = long_url
return f"Shortened URL: {request.url_root}{short_url}"
return render_template("index.html")
@app.route("/<short_url>")
def redirect_url(short_url):
long_url = shortened_urls.get(short_url)
if long_url:
return redirect(long_url)
else:
return "URL NOT FOUND", 404
if __name__ == '__main__':
app.run(debug=True)
now my html file name as “index.html” code is below
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>URL SHORTNER</title>
</head>
<body>
<h1>URL SHORTNER</h1>
<form method="POST" action="{{url_for('index)}}">
<input type="text" name="long_url" placeholder="Enter the long url">
<button type="submit">SHORTEN</button>
</form>
</body>
</html>
the template syntax error i am getting on the webpage is stated in the below image;
template syntax error(https://i.sstatic.net/ctikScgY.png)
New contributor
Tanishq Vernela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2