Ok I have checked some of the related questions and can't seem to find the exact one.
Basically I have pretty much hard-coded a website, there's a tiny bit of use of bootstrap.
I wanted to code a contact form, I've started to learn Python and therefore used Flask to code one.
Right now the form is separate to all of the html files because I coded a separate project.
I know that I could create a full Flask project and redefine all of my html files through the app and have the app generate URL's for it, but that would be a hell of a lot of work at this point for the sake of a contact form.
Is there any way that I can define the form in HTML and have it refer to the Python Flask script?
I have literally no idea how to do this and I can't find an answer anywhere on Google but then I might be searching the wrong thing.
So, here's my HTML:
<form action="routes.py" method="post" class="basic-grey">
<label>
<span>Your Name :</span>
<input id="name" type="text" name="name" placeholder="Your Full Name" />
</label>
<label>
<span>Your Email :</span>
<input id="email" type="email" name="email" placeholder="Valid Email Address" />
</label>
<label>
<span>Message :</span>
<textarea id="message" name="message" placeholder="Your Message to Us"></textarea>
</label>
<label>
<span> </span>
<input type="button" class="button" value="Send" />
</label>
</form>
And here is the Python Flask Script:
# Imported the Flask class and a function render_template
from flask import Flask, render_template, request, flash
from forms import ContactForm
from flask.ext.mail import Message, Mail
import os
mail = Mail()
# Created an instance/object of the Flask class
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Mapped the URL '/' to the function home().
# Now when someone visits this URL, the function home() will execute.
# This uses a decorator to tell Flask which URL will cause the function below it to execute.
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'fernindustrials@gmail.com'
app.config['MAIL_PASSWORD'] = 'l3d23pp3l1n'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail.init_app(app)
@app.route('/contact', methods=['GET','POST'])
def contact():
form = ContactForm()
if request.method == 'POST':
if form.validate() == False:
flash('All fields are required.')
return render_template('contact.html', form=form)
else:
msg = Message(form.subject.data, sender='contact@example.com')
msg.recipients = ["fernindustrials@gmail.com"]
msg.body="""
From: %s <%s>
%s
""" % (form.name.data, form.email.data, form.message.data)
mail.send(msg)
return render_template('posted.html')
elif request.method == 'GET':
return render_template('contact.html', form=form)
# Use run() to run our app on a local server.
if __name__ == '__main__':
app.run(debug = True)
In order to not breach the rules I wont post the Forms.py file that I have unless someone needs that to.
Is there anything I need to change or do differently? Is it even possible?
Also where does the script go in reference to the index.html files, i.e what is the project structure?
Aucun commentaire:
Enregistrer un commentaire