|
| 1 | +### **Day 27: Introduction to Web Development** |
| 2 | +Web development involves creating websites and web applications. Python can be used for web development with the help of web frameworks like Flask or Django. |
| 3 | + |
| 4 | +- **Understanding Web Development:** |
| 5 | + - Web development involves designing and building websites or web applications that are accessible through web browsers. |
| 6 | + - It encompasses both the front-end (user interface) and back-end (server-side) development. |
| 7 | + |
| 8 | +### **Day 28: Flask - Creating a Basic Web Application** |
| 9 | +Flask is a lightweight web framework for Python. You can create a basic web application with Flask. |
| 10 | + |
| 11 | +- **Getting Started with Flask:** |
| 12 | + - Install Flask using `pip install flask`. |
| 13 | + - Create a Flask application and define routes. |
| 14 | + - Use decorators like `@app.route('/')` to map URLs to view functions. |
| 15 | + - Start the development server using `app.run()`. |
| 16 | + |
| 17 | +**Example of a basic Flask web application:** |
| 18 | +```python |
| 19 | +from flask import Flask |
| 20 | + |
| 21 | +app = Flask(__name__) |
| 22 | + |
| 23 | +@app.route('/') |
| 24 | +def hello_world(): |
| 25 | + return 'Hello, World!' |
| 26 | + |
| 27 | +if __name__ == '__main__': |
| 28 | + app.run() |
| 29 | +``` |
| 30 | + |
| 31 | +### **Day 29: Flask - Adding Routes and Templates** |
| 32 | +Flask uses routes to map URLs to view functions. You can use templates to generate dynamic HTML content. |
| 33 | + |
| 34 | +- **Adding Routes and Templates:** |
| 35 | + - Define multiple routes for different URLs. |
| 36 | + - Use the `render_template` function to render HTML templates. |
| 37 | + - Pass data from the view function to the template. |
| 38 | + |
| 39 | +**Example of adding routes and using templates in Flask:** |
| 40 | +```python |
| 41 | +from flask import Flask, render_template |
| 42 | + |
| 43 | +app = Flask(__name__) |
| 44 | + |
| 45 | +@app.route('/') |
| 46 | +def index(): |
| 47 | + return 'Home Page' |
| 48 | + |
| 49 | +@app.route('/about') |
| 50 | +def about(): |
| 51 | + return 'About Us' |
| 52 | + |
| 53 | +@app.route('/contact') |
| 54 | +def contact(): |
| 55 | + return 'Contact Us' |
| 56 | + |
| 57 | +@app.route('/profile/<username>') |
| 58 | +def profile(username): |
| 59 | + return render_template('profile.html', username=username) |
| 60 | + |
| 61 | +if __name__ == '__main__': |
| 62 | + app.run() |
| 63 | +``` |
| 64 | + |
| 65 | +### **Day 30: Flask - Handling Forms and Data** |
| 66 | +Web applications often require handling user input through forms. You can use Flask to create forms and handle form data. |
| 67 | + |
| 68 | +- **Handling Forms in Flask:** |
| 69 | + - Create HTML forms with input fields. |
| 70 | + - Use the `request` object in Flask to access form data. |
| 71 | + - Handle form submissions and process user input. |
| 72 | + |
| 73 | +**Example of handling forms in Flask:** |
| 74 | +```python |
| 75 | +from flask import Flask, render_template, request |
| 76 | + |
| 77 | +app = Flask(__name__) |
| 78 | + |
| 79 | +@app.route('/') |
| 80 | +def index(): |
| 81 | + return render_template('index.html') |
| 82 | + |
| 83 | +@app.route('/submit', methods=['POST']) |
| 84 | +def submit(): |
| 85 | + if request.method == 'POST': |
| 86 | + name = request.form['name'] |
| 87 | + email = request.form['email'] |
| 88 | + return f'Thank you, {name}, for submitting your email: {email}' |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + app.run() |
| 92 | +``` |
| 93 | + |
| 94 | +Web development is a vast field, and Flask is an excellent starting point for building web applications with Python. However, for more extensive and feature-rich applications, you may consider learning Django, another popular Python web framework. These examples provide a foundation for creating web applications and handling user interactions using Flask. You can continue to explore and expand your web development skills from here. |
0 commit comments