-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.py
45 lines (36 loc) · 1.49 KB
/
contact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from flask import Flask, render_template, request, jsonify
import pymongo
from pymongo import MongoClient
app = Flask(__name__)
client = MongoClient('mongodb://localhost:27017/')
db = client['contact_form_db']
# MongoDB connection details
mongodb_host = 'localhost' # MongoDB server host
mongodb_port = 27017 # MongoDB server port
mongodb_database = 'contact_form_db' # Name of the database to connect to
# Establish connection to MongoDB server
client = pymongo.MongoClient(mongodb_host, mongodb_port)
# Access the specified database
db = client[mongodb_database]
# Route to render the contact_us.html template
@app.route('/')
def index():
# Fetch data from MongoDB (example)
data_from_mongodb = db.contacts.find()
return render_template('contact_us.html', data=data_from_mongodb)
# Route to handle form submission
@app.route('/submit-form', methods=['POST'])
def submit_form():
# Extract form data from the request
form_data = request.json
db.submissions.insert_one(form_data)
# Insert form data into MongoDB
# Example: db.your_collection_name.insert_one(form_data)
# Return a response indicating success
return jsonify({'message': 'Form submitted successfully'}), 200
@app.route('/get-submissions', methods=['GET'])
def get_submissions():
submissions = list(db.submissions.find()) # Retrieve all submissions from the database
return jsonify(submissions), 200
if __name__ == '__main__':
app.run(debug=True)