Added error messages and made the elements absolute positioned

This commit is contained in:
xemeds
2020-07-19 20:01:03 +00:00
parent cb286816fd
commit 1fa542b0cb
4 changed files with 55 additions and 30 deletions
+6 -4
View File
@@ -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")
+9 -16
View File
@@ -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,21 +7,14 @@ 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 # Create a instance of the form
if request.method == "GET": form = URLForm()
# Create a instance of the form
form = URLForm()
# If the form was valid
if form.validate_on_submit():
return "Valid URL: " + form.url.data
# If the form was invalid or not submitted
else:
# Return the index page with the form # Return the index page with the form
return render_template("index.html", form=form) return render_template("index.html", form=form)
# Post request
else:
# Create a instance of the form
form = URLForm()
# If the form was valid
if form.validate_on_submit():
return "Valid URL: " + form.url.data
return render_template("index.html", form=form)
+22 -5
View File
@@ -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;
}
+18 -5
View File
@@ -1,11 +1,19 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block body %} {% block body %}
<body onload="typeTitle()"> {% if form.url.errors %}
<div class="header"> <body>
<h1 id="title" class="title"></h1> <div class="header">
<h1 id="desc" class="desc"></h1> <h1 class="title">tiny0</h1>
</div> <h1 class="desc">Custom URL Shortener</h1>
</div>
{% else %}
<body onload="typeTitle()">
<div class="header">
<h1 id="title" class="title"></h1>
<h1 id="desc" class="desc"></h1>
</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>