Created
August 3, 2020 17:14
-
-
Save mmackz/2485cd3913e6550b259fd1b424de3f0d to your computer and use it in GitHub Desktop.
Intro to Flask: Tourist Attractions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, redirect, url_for | |
from locations import Locations | |
from forms import AddLocationForm | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = 'SECRET_PROJECT' | |
visit = Locations() | |
categories = {"recommended": "Recommended", "tovisit": "Places To Go", "visited": "Visited!!!", } | |
UP_ACTION = "\u2197" | |
DEL_ACTION = "X" | |
@app.route("/<category>", methods=["GET", "POST"]) | |
def locations(category): | |
locations = visit.get_list_by_category(category) | |
## Check the request for form data and process | |
if request.method == "POST": | |
[(name, action)] = request.form.items() | |
if action == UP_ACTION: | |
visit.moveup(name) | |
elif action == DEL_ACTION: | |
visit.delete(name) | |
## Return the main template with variables | |
return render_template("locations.html", category=category, categories=categories, locations=locations, add_location=AddLocationForm()) | |
@app.route("/add_location", methods=["POST"]) | |
def add_location(): | |
## Validate and collect the form data | |
add_form = AddLocationForm() | |
if add_form.validate_on_submit(): | |
name = add_form.name.data | |
description = add_form.description.data | |
category = add_form.category.data | |
visit.add(name, description, category) | |
## Redirect to locations route function | |
return redirect(url_for("locations", category=category)) | |
@app.route("/") | |
def index(): | |
## Redirect to locations route function | |
return redirect(url_for("locations", category="recommended")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask_wtf import FlaskForm | |
from wtforms import StringField, SubmitField, TextAreaField, RadioField | |
from wtforms.validators import DataRequired | |
categories = [("recommended","Recommended"), ("tovisit", "Places To Go"), ("visited", "Visited!!!")] | |
## Create Form Here | |
class AddLocationForm(FlaskForm): | |
name = StringField("Location Name", validators=[DataRequired()]) | |
description = TextAreaField("Location Description", validators=[DataRequired()]) | |
category = RadioField("Category", choices=categories) | |
submit = SubmitField("Add Location") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- extend from "base.html" here --> | |
{% extends "base.html" %} | |
<!-- begin block content here --> | |
{% block content %} | |
<h1>{{categories[category]}}</h1> <!-- insert category here --> | |
<div class="navbar"> | |
<!-- begin for loop here --> | |
{% for category, label in categories.items() %} | |
<a href="{{category}}">{{label}}</a> <!-- set attribute and text here --> | |
<!-- end for loop here --> | |
{% endfor %} | |
</div> | |
<table> | |
<colgroup> | |
<col style="width: 20%"> | |
<col style="width: 70%"> | |
<col style="width: 10%"> | |
</colgroup> | |
<tbody class="loctable"> | |
<!-- begin for loop here --> | |
{% for location in locations %} | |
<tr> | |
<td class="loc">{{location.name}}</td> <!-- insert location name here --> | |
<td class="desc">{{location.description}}</td> <!-- insert location description here --> | |
<td class="btns"> | |
<!-- start if statement here --> | |
{% if category in ["recommended", "tovisit"] %} | |
<form method="POST"> | |
<input type="submit" class="up" name="{{location.name}}" value=↗> <!-- set name attribute here --> | |
<input type="submit" class="del" name="{{location.name}}" value="X"> <!-- set name attribute here --> | |
</form> | |
<!-- end if statement here --> | |
{% endif %} | |
</td> | |
</tr> | |
<!-- end for loop here --> | |
{% endfor %} | |
</tbody> | |
</table> | |
<form class="addform" action="{{url_for('add_location')}}" method="POST"> <!-- set action attribute here --> | |
<!-- call hidden_tag() here --> | |
{{ add_location.hidden_tag() }} | |
<table> | |
<colgroup> | |
<col style="width: 40%"> | |
<col style="width: 40%"> | |
<col style="width: 20%"> | |
</colgroup> | |
<tbody> | |
<tr> | |
<td>{{add_location.name.label}}</td> <!-- insert location name label here --> | |
<td>{{add_location.description.label}}</td> <!-- insert location description label here --> | |
<td>{{add_location.category.label}}</td> <!-- insert location category label here --> | |
</tr> | |
<tr> | |
<td>{{add_location.name}}</td> <!-- insert add_location name here --> | |
<td>{{add_location.description}}</td> <!-- insert add_location description here --> | |
<td> | |
<!-- begin for loop here --> | |
{% for button in add_location.category %} | |
<div>{{button.label}}{{button()}}</div> <!-- insert button here --> | |
<!-- end for loop here --> | |
{% endfor %} | |
</td> | |
</tr> | |
<tr> | |
<td>{{add_location.submit()}}</td> <!-- insert submit here --> | |
</tr> | |
</tbody> | |
</table> | |
</form> | |
<!-- end block content here --> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment