News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

2024-11(8)

Web design based on Python language (hand-in-hand teaching you to design a personal blog website)

posted on 2024-11-02 14:08     read(678)     comment(0)     like(22)     collect(3)


 Overall design idea

The idea of ​​designing a web page involves many aspects, from the page structure and style on the front end to the data processing and logic implementation on the back end.

1. Determine the needs and functionality of your website

First of all, we need to clarify the functional requirements of the website, such as user registration and login, blog article publishing and display, comment system, etc.

2. Choose a technology stack

Choose the right framework and tools. For Python, common web frameworks include Flask and Django.

3. Set up the project structure

A reasonable project structure helps organize the code and facilitates subsequent maintenance and expansion.

4. Front-end design

The front end is mainly responsible for the display of web pages and user interaction, and can use HTML, CSS and JavaScript .

5. Backend design

The backend is responsible for business logic processing, database operations, user authentication, etc.

6. Database Design

Design the database model to determine the data that needs to be stored and its relationships.

7. Integrate front-end and back-end

The front-end and back-end are integrated through the API interface to realize data interaction.

8. Testing and Deployment

Perform sufficient testing to ensure that the functionality and performance meet the requirements before deploying to the server.

Practical Application

We can use the Flask framework to implement a simple blog website.

Step 1: Install Flask

First, you need to install Flask. You can install it using pip:

pip install flask

Step 2: Create the project structure

Create a project directory with the following structure:

  1. my_blog/
  2. ├── static/
  3. │ └── styles.css
  4. ├── templates/
  5. │ ├── layout.html
  6. │ ├── home.html
  7. │ └── post.html
  8. | └── new_post.html
  9. ├── app.py
  10. └── blogdata.py

Step 3: Set up the Flask application

Write the following code in app.py:

  1. from flask import Flask, render_template, request, redirect, url_for
  2. from blogdata import get_posts, get_post, add_post
  3. app = Flask(__name__)
  4. @app.route('/')
  5. def home():
  6. posts = get_posts()
  7. return render_template('home.html', posts=posts)
  8. @app.route('/post/<int:post_id>')
  9. def post(post_id):
  10. post = get_post(post_id)
  11. return render_template('post.html', post=post)
  12. @app.route('/new', methods=['GET', 'POST'])
  13. def new_post():
  14. if request.method == 'POST':
  15. title = request.form['title']
  16. content = request.form['content']
  17. add_post(title, content)
  18. return redirect(url_for('home'))
  19. return render_template('new_post.html')
  20. if __name__ == '__main__':
  21. app.run(debug=True)

Step 4: Create blog data processing

Simulate some blog data and operations in blogdata.py:

  1. posts = [
  2. {
  3. 'id': 1,
  4. 'title': 'First Post',
  5. 'content': 'This is the content of the first post.'
  6. },
  7. {
  8. 'id': 2,
  9. 'title': 'Second Post',
  10. 'content': 'This is the content of the second post.'
  11. }
  12. ]
  13. def get_posts():
  14. return posts
  15. def get_post(post_id):
  16. for post in posts:
  17. if post['id'] == post_id:
  18. return post
  19. return None
  20. def add_post(title, content):
  21. new_post = {
  22. 'id': len(posts) + 1,
  23. 'title': title,
  24. 'content': content
  25. }
  26. posts.append(new_post)
'
run

Step 5: Create HTML Template

Create the following HTML files in templatesthe directory:

layout.html
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>我的博客</title>
  7. <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
  8. </head>
  9. <body>
  10. <header>
  11. <h1>我的博客</h1>
  12. <nav>
  13. <a href="{{ url_for('home') }}">Home</a>
  14. <a href="{{ url_for('new_post') }}">New Post</a>
  15. </nav>
  16. </header>
  17. <main>
  18. {% block content %}{% endblock %}
  19. </main>
  20. <footer>
  21. <p>&copy; 2024 我的博客</p>
  22. </footer>
  23. </body>
  24. </html>

home.html

  1. {% extends 'layout.html' %}
  2. {% block content %}
  3. <h2>Blog Posts</h2>
  4. <ul>
  5. {% for post in posts %}
  6. <li>
  7. <a href="{{ url_for('post', post_id=post.id) }}">{{ post.title }}</a>
  8. </li>
  9. {% endfor %}
  10. </ul>
  11. {% endblock %}

post.html

  1. {% extends 'layout.html' %}
  2. {% block content %}
  3. <h2>{{ post.title }}</h2>
  4. <p>{{ post.content }}</p>
  5. <a href="{{ url_for('home') }}">Back to Home</a>
  6. {% endblock %}

new_post.html

  1. {% extends 'layout.html' %}
  2. {% block content %}
  3. <h2>New Post</h2>
  4. <form method="POST">
  5. <div>
  6. <label for="title">Title:</label>
  7. <input type="text" id="title" name="title">
  8. </div>
  9. <div>
  10. <label for="content">Content:</label>
  11. <textarea id="content" name="content"></textarea>
  12. </div>
  13. <button type="submit">Add Post</button>
  14. </form>
  15. {% endblock %}

Step 6: Create a style file

staticCreate in the directorystyles.css

  1. body {
  2. font-family: Arial, sans-serif;
  3. margin: 0;
  4. padding: 0;
  5. background-color: #f4f4f4;
  6. }
  7. header {
  8. background-color: #333;
  9. color: white;
  10. padding: 1rem;
  11. text-align: center;
  12. }
  13. nav a {
  14. color: white;
  15. margin: 0 1rem;
  16. text-decoration: none;
  17. }
  18. main {
  19. padding: 2rem;
  20. }
  21. footer {
  22. text-align: center;
  23. padding: 1rem;
  24. background-color: #333;
  25. color: white;
  26. position: fixed;
  27. bottom: 0;
  28. width: 100%;
  29. }

Running the Flask Application

Finally, run app.py in pycharm

Open your browser and visit http://127.0.0.1:5000, and you can see the blog page you created.



Category of website: technical article > Blog

Author:gfg

link:http://www.pythonblackhole.com/blog/article/245807/3de240cb5b5a88afb408/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

22 0
collect article
collected

Comment content: (supports up to 255 characters)