Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 8, 2021 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/9c9ac105ede15b869764318a3c38dc49 to your computer and use it in GitHub Desktop.
Save codecademydev/9c9ac105ede15b869764318a3c38dc49 to your computer and use it in GitHub Desktop.
Codecademy export
from flask import Flask, render_template, request, redirect, url_for
from locations import Locations
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, value)] = 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)
@app.route("/add_location", methods=["POST"])
def add_location():
## Validate and collect the form data
if True:
name=None
description=None
category=None
visit.add(name, description, category)
## Redirect to locations route function
return ""
@app.route("/")
def index():
## Redirect to locations route function
return ""
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField, RadioField
from wtforms.validators import DataRequired
class FieldsRequiredForm(FlaskForm):
"""Require radio fields to have content. This works around the bug that WTForms radio fields don't honor the `DataRequired` or `InputRequired` validators."""
class Meta:
def render_field(self, field, render_kw):
if field.type == "_Option":
render_kw.setdefault("required", True)
return super().render_field(field, render_kw)
categories = [("recommended","Recommended"), ("tovisit", "Places To Go"), ("visited", "Visited!!!")]
## Create Form Here
<!-- 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 location.category is in ["recommended", "tovisit"] %}
<form method="POST">
<input type="submit" class="up" name="" value=&#8599;> <!-- set name attribute here -->
{{ location.name }}
<input type="submit" class="del" name="" value="X"> <!-- set name attribute here -->
{{ location.name }}
</form>
<!-- end if statement here -->
{% endif %}
</td>
</tr>
<!-- end for loop here -->
{% endfor %}
</tbody>
</table>
<form class="addform" action="" method="POST"> <!-- set action attribute here -->
<!-- call hidden_tag() here -->
<table>
<colgroup>
<col style="width: 40%">
<col style="width: 40%">
<col style="width: 20%">
</colgroup>
<tbody>
<tr>
<td></td> <!-- insert location name label here -->
<td></td> <!-- insert location description label here -->
<td></td> <!-- insert location category label here -->
</tr>
<tr>
<td></td> <!-- insert add_location name here -->
<td></td> <!-- insert add_location description here -->
<td>
<!-- begin for loop here -->
<div></div> <!-- insert button here -->
<!-- end for loop here -->
</td>
</tr>
<tr>
<td></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