Skip to content

Commit dbd4bf8

Browse files
committed
Finishing
1 parent eaca742 commit dbd4bf8

File tree

5 files changed

+72
-98
lines changed

5 files changed

+72
-98
lines changed

ReadMe.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Flask-Blog
2+
This is a simple blog website built with Flask web framework. It uses Flask's built-in support for Jinja2 templating engine and SQLAlchemy for database management.
3+
4+
5+
## Usage
6+
After installing all the required dependencies by running "pip install -r requirements.txt."
7+
8+
You can access the blog website by visiting http://localhost:5000. use [email protected]:123456 to login as admin if you want to test the blog with this uploaded db.
9+
10+
The blog has the following features:
11+
12+
- User registration and login
13+
- Create, edit and delete blog posts (for admin only)
14+
- Create comments (Only for registred Users)
15+
- Contact page with contact form
16+
17+
18+
## Deployed Version
19+
Check out the deployed version of this blog [here](https://blog-site-5ojf.onrender.com/)!
20+
21+
22+
## Credits
23+
This blog is created by [Salmane A.]("https://github.com/saa1man"). Feel free to use it as a starting point for your own project.

main.py

+37-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from flask import Flask, render_template, redirect, url_for, flash, abort
1+
from flask import Flask, render_template, redirect, url_for, flash, abort, request
22
from flask_bootstrap import Bootstrap
33
from flask_ckeditor import CKEditor
4+
from flask_wtf.csrf import CSRFProtect
45
from datetime import date
56
from functools import wraps
67
from werkzeug.security import generate_password_hash, check_password_hash
@@ -10,10 +11,17 @@
1011
from forms import LoginForm, RegisterForm, CreatePostForm, CommentForm
1112
from flask_gravatar import Gravatar
1213
import os
14+
from email.mime.text import MIMEText
15+
from email.mime.multipart import MIMEMultipart
16+
import smtplib
17+
18+
EMAIL = os.environ.get("YOUR_EMAIL")
19+
PASSWORD = os.environ.get("YOUR_PASSWORD")
20+
SMTP_PROVIDER = os.environ.get("YOUR_SMTP_PROVIDER")
1321

1422
app = Flask(__name__)
15-
# app.config['SECRET_KEY'] = os.environ.get("SECRET_KEY") #Create your app app SECRET_KEY
16-
app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b'
23+
app.config['SECRET_KEY'] = os.environ.get("SECRET_KEY") #Create your own app SECRET_KEY
24+
csrf = CSRFProtect(app)
1725
ckeditor = CKEditor(app)
1826
Bootstrap(app)
1927
gravatar = Gravatar(app, size=100, rating='g', default='retro', force_default=False, force_lower=False, use_ssl=False, base_url=None)
@@ -164,11 +172,6 @@ def about():
164172
return render_template("about.html", current_user=current_user)
165173

166174

167-
@app.route("/contact")
168-
def contact():
169-
return render_template("contact.html", current_user=current_user)
170-
171-
172175
@app.route("/new-post", methods=["GET", "POST"])
173176
@admin_only
174177
def add_new_post():
@@ -189,8 +192,6 @@ def add_new_post():
189192
return render_template("make-post.html", form=form, current_user=current_user)
190193

191194

192-
193-
194195
@app.route("/edit-post/<int:post_id>", methods=["GET", "POST"])
195196
@admin_only
196197
def edit_post(post_id):
@@ -222,5 +223,30 @@ def delete_post(post_id):
222223
return redirect(url_for('get_all_posts'))
223224

224225

226+
@app.route("/contact", methods=["GET","POST"])
227+
def contact():
228+
if request.method == "POST":
229+
send_email(request.form['name'], request.form['email'],request.form['phone'],request.form['message'])
230+
return render_template("contact.html", current_user=current_user, msg_sent=True)
231+
return render_template("contact.html", current_user=current_user, msg_sent=False)
232+
233+
def send_email(name, email, phone, message):
234+
# Create a new SMTP object
235+
smtp_server = smtplib.SMTP(SMTP_PROVIDER, 587)
236+
smtp_server.starttls()
237+
smtp_server.login(EMAIL, PASSWORD)
238+
# Create the message
239+
msg = MIMEMultipart()
240+
msg['From'] = EMAIL
241+
msg['To'] = EMAIL
242+
msg['Subject'] = 'New Contact Form The Blog'
243+
body = f"Name: {name}\nEmail: {email}\nPhone: {phone}\nMessage: {message}"
244+
msg.attach(MIMEText(body, 'plain'))
245+
# Send the message
246+
smtp_server.send_message(msg)
247+
# Close the SMTP connection
248+
smtp_server.quit()
249+
250+
225251
if __name__ == "__main__":
226-
app.run(host='0.0.0.0', port=5000)
252+
app.run(host='0.0.0.0', port=5000)

static/js/contact_me.js

-75
This file was deleted.

templates/contact.html

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{% include "header.html" %}
2-
32
<!-- Page Header -->
43
<header class="masthead" style="background-image: url('{{ url_for('static', filename='img/contact-bg.jpg')}}')">
54
<div class="overlay"></div>
65
<div class="container">
76
<div class="row">
87
<div class="col-lg-8 col-md-10 mx-auto">
98
<div class="page-heading">
10-
<h1>Contact Me</h1>
9+
{% if msg_sent: %}
10+
<h1>Successfully sent your message</h1>
11+
{% else %}
12+
<h1>Contact Me</h1>
13+
{% endif %}
1114
<span class="subheading">Have questions? I have answers.</span>
1215
</div>
1316
</div>
@@ -20,40 +23,38 @@ <h1>Contact Me</h1>
2023
<div class="row">
2124
<div class="col-lg-8 col-md-10 mx-auto">
2225
<p>Want to get in touch? Fill out the form below to send me a message and I will get back to you as soon as possible!</p>
23-
<!-- Contact Form - Enter your email address on line 19 of the mail/contact_me.php file to make this form work. -->
24-
<!-- WARNING: Some web hosts do not allow emails to be sent through forms to common mail hosts like Gmail or Yahoo. It's recommended that you use a private domain email address! -->
25-
<!-- To use the contact form, your site must be on a live web host with PHP! The form will not work locally! -->
26-
<form name="sentMessage" id="contactForm" novalidate>
26+
<form name="sentMessage" id="contactForm" action="{{ url_for('contact') }}" method="post" novalidate>
27+
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
2728
<div class="control-group">
2829
<div class="form-group floating-label-form-group controls">
2930
<label>Name</label>
30-
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
31+
<input type="text" class="form-control" placeholder="Name" id="name" name="name" required data-validation-required-message="Please enter your name.">
3132
<p class="help-block text-danger"></p>
3233
</div>
3334
</div>
3435
<div class="control-group">
3536
<div class="form-group floating-label-form-group controls">
3637
<label>Email Address</label>
37-
<input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
38+
<input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required data-validation-required-message="Please enter your email address.">
3839
<p class="help-block text-danger"></p>
3940
</div>
4041
</div>
4142
<div class="control-group">
4243
<div class="form-group col-xs-12 floating-label-form-group controls">
4344
<label>Phone Number</label>
44-
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
45+
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" name="phone" required data-validation-required-message="Please enter your phone number.">
4546
<p class="help-block text-danger"></p>
4647
</div>
4748
</div>
4849
<div class="control-group">
4950
<div class="form-group floating-label-form-group controls">
5051
<label>Message</label>
51-
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
52+
<textarea rows="5" class="form-control" placeholder="Message" id="message" name="message" required data-validation-required-message="Please enter a message."></textarea>
5253
<p class="help-block text-danger"></p>
5354
</div>
5455
</div>
5556
<br>
56-
<div id="success"></div>
57+
<!--<div id="success"></div>-->
5758
<button type="submit" class="btn btn-primary" id="sendMessageButton">Send</button>
5859
</form>
5960
</div>

templates/footer.html

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
<script>
4545
document.getElementById("year").innerHTML = new Date().getFullYear();
4646
</script>
47-
4847
</body>
4948

5049
</html>

0 commit comments

Comments
 (0)