These are
Python Web Development fundamentals for developers familiar with PHP and JavaScript.
Core Frameworks
-
Flask: Lightweight, similar to Express.js. Great for APIs and small apps.
-
Django: Full-featured like Laravel/WordPress. Includes ORM, admin panel, auth system.
-
FastAPI: Modern, high-performance framework with automatic API documentation.
Key Concepts
- Python uses indentation (not braces) for code blocks
- No semicolons needed
- Function-based views vs class-based views (similar to PHP procedural vs OOP)
Simple Flask Example (Compare to PHP)
```python
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/') # Like PHP routing or .htaccess rules
def home():
return render_template('home.html', title='Home') # Similar to PHP include/require
@app.route('/user/<username>') # Dynamic URL parameter (like PHP $_GET)
def profile(username):
return f"Profile for {username}" # f-strings are Python's template literals
@app.route('/form', methods=['POST'])
def handle_form():
data = request.form # Similar to PHP $_POST
return f"Received: {data['name']}"
```
Database Access (vs PHP/MySQL)
```python
# Using Django ORM (compare to raw PHP MySQL)
from django.db import models
class Post(models.Model): # Similar to creating a DB table in PHP
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField('date published')
Query example
posts = Post.objects.filter(pub_date__year=2023).order_by('-pub_date')
# vs PHP: SELECT * FROM posts WHERE YEAR(pub_date) = 2023 ORDER BY pub_date DESC
```
Template System (vs PHP/HTML)
```html
<!-- Django template (compare to PHP in HTML) -->
<h1>{{ post.title }}</h1> <!-- Like <?php echo $post->title; ?> -->
{% for comment in comments %} <!-- Like <?php foreach($comments as $comment): ?> -->
<p>{{ comment.text }}</p>
{% endfor %} <!-- Like <?php endforeach; ?> -->
```
Python web development uses more abstraction than PHP but has similar concepts to what you already know.
In PHP, you often work directly with the web server's request/response cycle. PHP files typically mix HTML and PHP code, with PHP handling server-side logic directly embedded in presentation markup. This is a more direct approach - the connection between your code and the HTTP mechanics is quite visible.
Python web development, by contrast, introduces more layers of abstraction:
- Framework-driven approach: Most Python web development happens through frameworks like Django or Flask that abstract away many HTTP details. Rather than writing code that directly outputs HTML, you define route handlers, views, and templates as separate components.
- Separation of concerns: Python web frameworks strongly encourage separating business logic, data access, and presentation. While PHP has frameworks that do this too (Laravel, Symfony), Python frameworks make this separation more fundamental.
- ORM prevalence: Python web development heavily relies on Object-Relational Mappers. While PHP has options like Doctrine, Python developers almost always use ORMs (Django ORM, SQLAlchemy) rather than writing raw SQL.
- Configuration over convention: Python frameworks often use decorators, configuration files, and class inheritance to define application behavior, versus PHP's more direct procedural approach in many cases.
Despite these differences, the underlying concepts remain similar: routes map to functions/methods, templates render data, database operations persist information, etc.
If you understand PHP's web development model, you'll recognize these patterns in Python - they're just wrapped in different syntax and architectural approaches.
No comments:
Post a Comment