Added error messages and made the elements absolute positioned
This commit is contained in:
+6
-4
@@ -12,17 +12,17 @@ def validate_URL(form, field):
|
|||||||
# If the url contains spaces or does not have any dots
|
# If the url contains spaces or does not have any dots
|
||||||
if field.data.count(" ") > 0 or field.data.count(".") == 0:
|
if field.data.count(" ") > 0 or field.data.count(".") == 0:
|
||||||
# Raise a ValidationError
|
# Raise a ValidationError
|
||||||
raise ValidationError()
|
raise ValidationError("Invalid URL")
|
||||||
|
|
||||||
# If the url starts with a dot after http:// or after https:// or just starts with a dot
|
# If the url starts with a dot after http:// or after https:// or just starts with a dot
|
||||||
if field.data.startswith("http://.") or field.data.startswith("https://.") or field.data.startswith("."):
|
if field.data.startswith("http://.") or field.data.startswith("https://.") or field.data.startswith("."):
|
||||||
# Raise a ValidationError
|
# Raise a ValidationError
|
||||||
raise ValidationError()
|
raise ValidationError("Invalid URL")
|
||||||
|
|
||||||
# If the url ends with a dot and it is the only dot
|
# If the url ends with a dot and it is the only dot
|
||||||
if field.data.endswith(".") and field.data.count(".") == 1:
|
if field.data.endswith(".") and field.data.count(".") == 1:
|
||||||
# Raise a ValidationError
|
# Raise a ValidationError
|
||||||
raise ValidationError()
|
raise ValidationError("Invalid URL")
|
||||||
|
|
||||||
# If the URL does not start with http:// and https://
|
# If the URL does not start with http:// and https://
|
||||||
if not(field.data.startswith("http://")) and not(field.data.startswith("https://")):
|
if not(field.data.startswith("http://")) and not(field.data.startswith("https://")):
|
||||||
@@ -31,6 +31,8 @@ def validate_URL(form, field):
|
|||||||
|
|
||||||
|
|
||||||
class URLForm(FlaskForm):
|
class URLForm(FlaskForm):
|
||||||
url = StringField(validators=[DataRequired(), Length(min=4, max=2000), validate_URL])
|
url = StringField(validators=[DataRequired(),
|
||||||
|
Length(min=4, max=2000, message="URL must be between %(min)d and %(max)d characters"),
|
||||||
|
validate_URL])
|
||||||
|
|
||||||
submit = SubmitField("Shorten")
|
submit = SubmitField("Shorten")
|
||||||
|
|||||||
+4
-11
@@ -1,4 +1,4 @@
|
|||||||
from flask import render_template, redirect, request, flash, url_for
|
from flask import render_template, redirect, request, url_for
|
||||||
from tiny0 import app
|
from tiny0 import app
|
||||||
from tiny0.forms import URLForm
|
from tiny0.forms import URLForm
|
||||||
#from tiny0.models import URLs
|
#from tiny0.models import URLs
|
||||||
@@ -7,16 +7,6 @@ from tiny0.forms import URLForm
|
|||||||
# Index Page
|
# Index Page
|
||||||
@app.route("/", methods=['GET', 'POST'])
|
@app.route("/", methods=['GET', 'POST'])
|
||||||
def index():
|
def index():
|
||||||
# Get request
|
|
||||||
if request.method == "GET":
|
|
||||||
# Create a instance of the form
|
|
||||||
form = URLForm()
|
|
||||||
|
|
||||||
# Return the index page with the form
|
|
||||||
return render_template("index.html", form=form)
|
|
||||||
|
|
||||||
# Post request
|
|
||||||
else:
|
|
||||||
# Create a instance of the form
|
# Create a instance of the form
|
||||||
form = URLForm()
|
form = URLForm()
|
||||||
|
|
||||||
@@ -24,4 +14,7 @@ def index():
|
|||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
return "Valid URL: " + form.url.data
|
return "Valid URL: " + form.url.data
|
||||||
|
|
||||||
|
# If the form was invalid or not submitted
|
||||||
|
else:
|
||||||
|
# Return the index page with the form
|
||||||
return render_template("index.html", form=form)
|
return render_template("index.html", form=form)
|
||||||
|
|||||||
+22
-5
@@ -10,28 +10,37 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.header {
|
.header {
|
||||||
color: #ffffff;
|
position: absolute;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
top: 3vh;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
margin-top: 5%;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.title {
|
.title {
|
||||||
font-weight: bold;
|
color: #ffffff;
|
||||||
font-size: 50px;
|
font-size: 50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.desc {
|
.desc {
|
||||||
margin-top: 1%;
|
color: #ffffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
.container {
|
.container {
|
||||||
|
position: absolute;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
background-color: #2c2c2c;
|
background-color: #2c2c2c;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
padding: 30px 0px;
|
padding: 30px 0px;
|
||||||
box-shadow: 0px 10px 20px #000000;
|
box-shadow: 0px 10px 20px #000000;
|
||||||
width: 80vw;
|
width: 80vw;
|
||||||
height: auto;
|
height: auto;
|
||||||
margin: 10% auto 0 auto;
|
margin: 35vh auto 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.inputs {
|
.inputs {
|
||||||
@@ -66,3 +75,11 @@ body {
|
|||||||
background: #7b59a5;
|
background: #7b59a5;
|
||||||
border-color: #7b59a5;
|
border-color: #7b59a5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error-message {
|
||||||
|
color: #ffffff;
|
||||||
|
text-align: center;
|
||||||
|
font-size: 100%;
|
||||||
|
padding: 0;
|
||||||
|
margin: 2% 0 -1% 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
{% extends "layout.html" %}
|
{% extends "layout.html" %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
{% if form.url.errors %}
|
||||||
|
<body>
|
||||||
|
<div class="header">
|
||||||
|
<h1 class="title">tiny0</h1>
|
||||||
|
<h1 class="desc">Custom URL Shortener</h1>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
<body onload="typeTitle()">
|
<body onload="typeTitle()">
|
||||||
<div class="header">
|
<div class="header">
|
||||||
<h1 id="title" class="title"></h1>
|
<h1 id="title" class="title"></h1>
|
||||||
<h1 id="desc" class="desc"></h1>
|
<h1 id="desc" class="desc"></h1>
|
||||||
</div>
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<form method="POST" action="">
|
<form method="POST" action="">
|
||||||
@@ -15,6 +23,11 @@
|
|||||||
{{ form.submit(class="submit") }}
|
{{ form.submit(class="submit") }}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
{% if form.url.errors %}
|
||||||
|
{% for error in form.url.errors %}
|
||||||
|
<p class="error-message">{{ error }}</p>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
Reference in New Issue
Block a user