Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Add permanent links to a page based on form entries

I would like to add links to a front page based on information returned from a form. For instance I would like users to be able to access a home page which would have a list of things and the option to create a new thing. They can click or select one of the things in the list and it will bring up a new page with information about that thing or they can click the link to create a new a thing. Clicking create will take them to a form they will need to fill out and submit. As part of the form submission I would like to take some of the information they submitted in the form to another entry in the list of things on the home page so that information about this new thing can be accessed directly in the future.

I've worked out the home page and the create link. I've worked out some elements of the form and the submit button. I've worked out creating a cookie for the form data and sending that to the home page, but I can't figure out how to add the information to a static list? My problem is I can get it to read the cookie to temporarily create a list item, but the items won't be in the cookie if someone accesses the home page directly without first creating the thing.

The second problem I have is that I want to display the list name as a string joining various elements from the from. I have the code for pulling the items from the key, but its displaying the code and not the values of the code. For example, I would like the list item to be displayed as acme-1234-!@#$, which would be the values of field 1, field 2, and field 3. I've entered the code to pull that out, but its displaying the code instead of the values.

Im using Python 3.5 and Flask

Here is what I have in my awsdc_main.py

import json

from flask import (Flask, render_template, redirect, url_for, request, make_response)

app = Flask(__name__)

def get_saved_data():
    try:
        data = json.loads(request.cookies.get('aws_attributes'))
    except TypeError:
        data = {}
    return data

@app.route('/', methods=['POST', 'GET'])
def home():
    return render_template('awsdc_main.html', aws_attributes = get_saved_data())

@app.route('/Create_DirectConnect')
def create_directconnect():
    return render_template('awsdc_make_new.html')

@app.route('/makeitso', methods=['POST'])
def makeitso():
    #getting cookie
    aws_attributes = get_saved_data()
    aws_attributes.update(dict(request.form.items()))
    #setting cookie
    response = make_response(redirect(url_for('home')))
    #response = make_response(redirect(url_for('awsdc_page',
    #                name='AWS-' + aws_attributes.get('company_name')
    #                + '-' + aws_attributes.get('vpc_name').upper())))
    response.set_cookie('aws_attributes', json.dumps(dict(request.form.items())))
    #setting filename to be AWS-<company_name>-<vpc_name>
    awsdc_name = 'AWS-' + aws_attributes.get('company_name') + '-' + aws_attributes.get('vpc_name').upper()
    # creating flat file with form values
    with open('{}.py'.format(awsdc_name), 'w') as file:
        file.write(json.dumps(aws_attributes))
    return response



@app.route('/<name>')
def awsdc_page(name):
    return render_template('awsdc_page.html', aws_attributes=get_saved_data())



app.run(debug=True, port=8000, host='0.0.0.0')

and here is what I have in my awsdc_main.html file which will be the home page.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>AWS DirectConnect Connections</title>
     <link rel="stylesheet" href="/static/stylesheet.css">
  </head>
  <header>
    <h1>
    AWS DirectConnect Connections
    </h1>
  </header>
  <body>
  <a href="{{url_for('create_directconnect')}}">Create a new DirectConnect.</a>
  <div>
    <form action="{{url_for('awsdc_page', name='AWS-' + aws_attributes.get('company_name') + '-' + aws_attributes.get('vpc_name').upper())}}" method="POST">
      <select name='DirectConnect_name'>
        <option value="none">None</option>
      <option value="'AWS-' + aws_attributes.get('company_name') + '-' + aws_attributes.get('vpc_name').upper()">
                     'AWS-' + aws_attributes.get('company_name') + '-' + aws_attributes.get('vpc_name').upper()</option>
      </select>
    </form>
  </div>
  </body>
</html>

I am extremely new to all of this. I have gone through the Python Basics course, the Flask Basics course, and several others. For quickness and simplicity sake, I was trying to do this with a flat file instead of database, but I've given up on that idea. I think it will actually be easier create a database to store all the form values in and recall them instead of individual flat files for each page.

Any help or advice is appreciated.