Using Flask
Flask is a micro-framework for Python, meaning it’s lightweight and easy to use for small to medium-sized applications.
Install Flask:
bashpip install Flask
Create a basic Flask application:
pythonfrom flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Flask!" if __name__ == "__main__": app.run(debug=True)
Run the application: Save the file (e.g.,
app.py
), then run it from the command line:bashpython app.py
View the website: Open your browser and go to
http://127.0.0.1:5000/
to see your "Hello, Flask!" message.
Using Django
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.
Install Django:
bashpip install Django
Create a Django project:
bashdjango-admin startproject myproject cd myproject
Create a Django app:
bashpython manage.py startapp myapp
Define a view in
myapp/views.py
:pythonfrom django.http import HttpResponse def home(request): return HttpResponse("Hello, Django!")
Configure the URL in
myproject/urls.py
:pythonfrom django.contrib import admin from django.urls import path from myapp import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.home), ]
Run the development server:
bashpython manage.py runserver
View the website: Open your browser and go to
http://127.0.0.1:8000/
to see your "Hello, Django!" message.
Additional Steps
For both frameworks, you can further develop your website by:
- Adding templates: HTML files that define the structure of your web pages.
- Adding static files: CSS, JavaScript, and image files to style and add interactivity to your website.
- Creating models: Define your data structure and interact with the database.
- Implementing forms: Handle user input and submissions.
0 Comments