0% found this document useful (0 votes)
3 views5 pages

Project Structure Python Flask

The document outlines the structure and components of a Flask application that integrates with MongoDB for user registration. It includes an HTML form for user input, a Python backend to handle form submissions and store user data in a MongoDB database, and instructions for installing required packages and running the application. Users can access the registration form via a web browser after starting the Flask app.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Project Structure Python Flask

The document outlines the structure and components of a Flask application that integrates with MongoDB for user registration. It includes an HTML form for user input, a Python backend to handle form submissions and store user data in a MongoDB database, and instructions for installing required packages and running the application. Users can access the registration form via a web browser after starting the Flask app.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Project Structure

flask_mongodb_app/

│── templates/

│ ├── [Link]

│── [Link]

│── [Link]

1. [Link] (Form Page)

Save this inside the templates/ folder.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Flask MongoDB Form</title>

</head>

<body>

<h2>User Registration</h2>

<form action="/submit" method="post">

<label for="fullname">Full Name:</label>

<input type="text" name="fullname" required><br><br>


<label for="email">Email:</label>

<input type="email" name="email" required><br><br>

<label for="password">Password:</label>

<input type="password" name="password" required><br><br>

<input type="submit" value="Register">

</form>

</body>

</html>

2. [Link] (Flask Backend)

Save this in the root folder (flask_mongodb_app/).

from flask import Flask, render_template, request, redirect, url_for

from pymongo import MongoClient

from [Link] import generate_password_hash

app = Flask(__name__)

# Connect to MongoDB

client = MongoClient("mongodb://localhost:27017/")

db = client["user_database"]

collection = db["users"]

@[Link]("/")

def index():

return render_template("[Link]")

@[Link]("/submit", methods=["POST"])

def submit():

if [Link] == "POST":

fullname = [Link]["fullname"]

email = [Link]["email"]

password = generate_password_hash([Link]["password"])
user_data = {

"full_name": fullname,

"email": email,

"password": password

collection.insert_one(user_data)

return "Registration successful!"

if __name__ == "__main__":

[Link](debug=True)

3. Install Required Packages

Create a [Link] file and add:


Flask

pymongo

werkzeug

Then, install dependencies by running:

pip install -r [Link]

4. Run the Flask App

Start MongoDB if it's not running:

Run the Flask app:

python [Link]

Open your browser and go to:


[Link]
Fill the form in browser

Data Stored in MongoDB

You might also like