Python for Web Development: How to Build Dynamic Websites with Python This repository provides resources for building dynamic websites using Python. The materials cover essential web development concepts, including server-side scripting, database management, and front-end development.
Requirements
- Python 3.x
- Flask
- SQLALCHEMY
- Jinja2
- HTML, CSS, and JavaScript
Installation
- Clone this repository to your local machine
- Install required packages using pip install -r requirements.txt
- Start the server using python app.py
- Navigate to https://localhost:5000 in your web browser
1. Create your own Flask application by importing the necessary libraries and setting up a new Flask instance.
from flask import Flask, render_template
app = Flask(name)
@app.route('/')
def index():
return render_template('index.html')
2. Add routes to your application to handle different URL requests.
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
3.Create templates for each of your routes using Jinja2 syntax.
<!doctype html>
<title>Welcome to my website</title>
This is the home page for my website.
4.Use SQLALCHEMY to manage your database and store data from user interactions.
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:https:///mydatabase.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(50), nullable=False)
message = db.Column(db.String(200), nullable=False)
def repr(self):
return f'<User {self.name}>'
5. Use HTML, CSS, and JavaScript to style your website and create dynamic user interfaces.
Contributions are welcome! Please open a pull request or file an issue if you find a bug or have a feature request.
lesson from Python Training