


Django development: How to create a beautiful web application using Python and Django
Django is a popular Python web application framework that provides a powerful set of tools and engines that can help us easily build beautiful, scalable web applications.
In this article, we will introduce how to create a beautiful web application using Python and Django. We'll start by installing Django and creating a brand new Django project. Next, we'll create a simple web application and introduce how to create database models, views, and templates. Finally, we will add some styles and animations to this application to make it prettier.
1. Install Django
Before we start creating our web application, we need to install Django on the local computer. Django can be installed through the following command:
pip install Django
If you do not have pip installed, please install pip first. pip is a Python package manager that allows you to easily manage Python libraries and dependencies.
After the installation is complete, you can check whether Django has been installed successfully by running the following command:
django-admin --version
If Django has been installed successfully, you will see the version number of Django.
2. Create a new Django project
Now that we have Django installed, we can start creating our web application. First, we need to create a new Django project. A new Django project can be created with the following command:
django-admin startproject myproject
This command will create a new directory called "myproject" and create the basic structure of a Django project in it.
3. Create a simple web application
We have created a new Django project, now we can start creating our web application. We will create a simple web application that will allow users to publish and view posts on the website.
In order to create this web application, we need to perform the following steps:
1. Create a new Django application
2. Create a database model
3. Create views and Template
First, we will create a new Django application. A new Django application can be created with the following command:
python manage.py startapp myapp
This command will create a new directory called "myapp" and create the basic structure of a Django application within it.
Next, we need to create a database model. Our database model will define the Post object and describe the fields and properties of the Post object.
Create a file named "models.py" in the myapp directory and add the following code:
from django.db import models class Post(models.Model): title = models.CharField(max_length=100) content = models.TextField() pub_date = models.DateTimeField('date published')
In the above code, we define a file named "Post" Object, which has three attributes: title, content and pub_date.
The title and content attributes are fields of CharField and TextField type. They will store our post title and content. The pub_date attribute is a field of type DateTimeField that will store the time our post was published.
Next, we need to create views and templates. Views are where web applications handle requests, and templates determine how our web applications should be rendered in the user's browser.
Create a file named "views.py" in the myapp directory and add the following code:
from django.shortcuts import render from django.http import HttpResponse from .models import Post def index(request): latest_posts = Post.objects.order_by('-pub_date')[:5] context = {'latest_posts': latest_posts} return render(request, 'myapp/index.html', context) def detail(request, post_id): post = Post.objects.get(pk=post_id) return render(request, 'myapp/detail.html', {'post': post})
In the above code, we define two views: index and detail . The index view will query the database for the 5 most recent posts and pass them to the template. The detail view queries the database for a specific post based on post_id and passes it to the template.
Next, we need to create the template. Create a new directory named "templates" under the myapp directory and add two HTML template files "myapp/index.html" and "myapp/detail.html".
In the "myapp/index.html" template, we will render the last 5 articles:
{% for post in latest_posts %} <div class="post"> <h2 id="post-title">{{ post.title }}</h2> <p class="date">{{ post.pub_date }}</p> <p class="content">{{ post.content }}</p> </div> {% endfor %}
In the "myapp/detail.html" template, we will render the specific article :
<div class="post"> <h2 id="post-title">{{ post.title }}</h2> <p class="date">{{ post.pub_date }}</p> <p class="content">{{ post.content }}</p> </div>
Now that we have created a simple Django web application, we can run it and see the effects. Enter the following command in the terminal to start the server:
python manage.py runserver
Open the browser and enter http://localhost:8000/myapp/, you will see a list of the 5 most recently published articles.
4. Add styles and animations
Now that we have created a simple web application, let's add some styles and animations to it. We will use Bootstrap framework and jQuery library to achieve this task.
First, we need to add static files to the application. Create a new directory called "static" under the myapp directory and create another directory called "myapp" inside it. Under the "myapp" directory, we will add two subdirectories, css and js. Under these two subdirectories, we will add files named "style.css" and "script.js".
In the "style.css" file, we will add some basic styles:
.post { background-color: #fff; border: 1px solid #ccc; margin-bottom: 20px; padding: 10px; } .title { color: #ff0000; font-size: 24px; font-weight: bold; } .date { color: #00ff00; font-size: 14px; font-style: italic; } .content { color: #0000ff; font-size: 16px; }
In the "script.js" file, we will add some basic animations:
$(document).ready(function() { $('.post').hover(function() { $(this).animate({ backgroundColor: "#FEEBD4" }, 200); }, function() { $(this).animate({ backgroundColor: "#fff" }, 200); }); });
In the above code, we use the jQuery library to change the background color of each article from white to pink when the user hovers over it:
Now that we have added styles and animations to our Django web application, let's run it and see the effect! Open the browser and enter http://localhost:8000/myapp/. You will see a list of 5 recently published articles, all of which have been modified. As you hover over each article, their background color will change to pink, making them look prettier.
Conclusion
In this article, we covered how to create a beautiful web application using Python and Django. We started by installing Django, creating a new Django project, and creating a simple web application. We used Django's database model to define the Post object, and used Django's views and templates to present and query the data. Finally, we also added some styles and animations to the web application to make it look prettier.
Django is a powerful Python web application framework that helps us easily build beautiful, scalable web applications. If you're considering building your own web application using Python and Django, these tips should help you get started quickly.
The above is the detailed content of Django development: How to create a beautiful web application using Python and Django. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software