Creating a basic website using Python can be done with the help of a web framework like Flask or Django. Flask is great for small projects due to its simplicity, while Django is better for larger, more complex applications due to its built-in features.
Here's an example of how to create a simple website using Flask:
First, you need to install Flask. You can do this using pip:
pip install FlaskCreate a Python file, say app.py, and write the following code:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)Flask uses the Jinja2 templating engine, which allows you to write HTML files that can be dynamically populated with data. Create a folder called templates in the same directory as app.py, and inside this folder, create two HTML files: index.html and about.html.
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is the home page.</p>
<a href="/about">About</a>
</body>
</html>about.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About</title>
</head>
<body>
<h1>About Us</h1>
<p>This is the about page.</p>
<a href="/">Home</a>
</body>
</html>Run your Flask application by executing the app.py file:
python app.pyThis will start a local web server. You can visit your website by opening a web browser and going to http://127.0.0.1:5000/.
From here, you can add more routes, templates, and functionality to your website. Flask is very flexible, allowing you to create complex websites by adding forms, handling user input, interacting with databases, and more.
If you're looking for something more robust with built-in admin panels, user authentication, and a variety of other features, Django might be a better choice. The setup for Django is more complex, but it's well-suited for larger applications.
Would you like to dive deeper into any specific part of this process?