Added requirements for the project, added WTForms for contact form validation, and finished setting up the contact form. Now ready to go into production for testing there.

This commit is contained in:
William Brawner 2016-03-09 21:42:37 -06:00
parent a13ca0a81b
commit 1e155009f6
6 changed files with 50 additions and 13 deletions

View file

@ -1,8 +1,9 @@
MYSQL_USER = 'user'
MYSQL_PASSWORD = 'password'
MYSQL_HOST = 'localhost'
MYSQL_HOST = '%'
MYSQL_DB = 'database'
DEBUG = True
SECRET_KEY = 'development key'
SECRET_KEY = '\xd9\n\xe6[Hy\xf6\xac\x00\xd3b\xc2\xb3\x98Ii\x8e\x1b\xcb\xcc\x89'K\x1f'
USERNAME = 'username'
PASSWORD = 'password'
MAIL_DEFAULT_SENDER = "admin@example.com"

View file

@ -5,6 +5,7 @@ import hashlib
from flask_debugtoolbar import DebugToolbarExtension
from admin import admin
from flask.ext.mail import Mail, Message
from wtforms import Form, TextField, TextAreaField, validators
app = Flask(__name__)
app.config.from_pyfile('config.py')
@ -28,6 +29,11 @@ def teardown_request(exception):
if db is not None:
db.close()
class ContactForm(Form):
name = TextField('Name', [validators.Length(min=4, max=25)])
email = TextField('Email Address', [validators.Length(min=6, max=35)])
message = TextAreaField('Message', [validators.Length(min=6, max=5000)])
@app.route('/')
def home():
g.db.execute('SELECT * FROM blog_posts ORDER BY updated_on DESC')
@ -57,12 +63,16 @@ def projects():
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
request.form['name']
request.form['email']
request.form['message']
form = ContactForm(request.form)
if request.method == 'POST' and form.validate():
msg = Message(subject='New Message From wbrawner.com',
body="Name: {0}\nEmail: {1}\nMessage: {2}".format(request.form['name'], request.form['email'], request.form['message']),
recipients=["billybrawner@gmail.com"])
mail.send(msg)
flash('Thanks, your message was sent.')
return redirect(url_for('contact'))
else:
return render_template('contact.html')
return render_template('contact.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():

11
requirements.txt Normal file
View file

@ -0,0 +1,11 @@
blinker==1.4
Flask==0.10.1
Flask-DebugToolbar==0.10.0
Flask-Mail==0.9.1
Flask-MySQLdb==0.2.0
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
mysqlclient==1.3.7
Werkzeug==0.11.4
WTForms==2.1

View file

@ -4,7 +4,7 @@ CREATE TABLE blog_posts (
text VARCHAR( 10000 ) DEFAULT NULL ,
category VARCHAR( 50 ) DEFAULT NULL ,
tags VARCHAR( 100 ) DEFAULT NULL ,
url VARCHAR(500) DEFAULT NULL UNIQUE,
url VARCHAR(100) DEFAULT NULL UNIQUE,
created_on DATETIME NOT NULL ,
updated_on TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP
);

View file

@ -0,0 +1,12 @@
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}

View file

@ -2,11 +2,14 @@
{% block body %}
<div class="section" id="contact">
<h2>contact</h2>
<form method="{{ url_for('contact') }}">
<input type="text" name="name">
<input type="text" name="email">
<textarea name="message"></textarea>
<input type="submit" value="submit">
{% from "_formhelper.html" import render_field %}
<form method="post" action="{{ url_for('contact') }}">
<dl>
{{ render_field(form.name) }}
{{ render_field(form.email) }}
{{ render_field(form.message) }}
</dl>
<p style="margin-left: 40px;"><input type="submit" value="Send">
</form>
<div class="one-half">
<a class="twitter-timeline" href="https://twitter.com/BillyBrawner1" data-widget-id="706308392311136256">Tweets by @BillyBrawner1</a>