Home > Backend Development > Python Tutorial > Django development: How to create a beautiful web application using Python and Django

Django development: How to create a beautiful web application using Python and Django

WBOY
Release: 2023-06-22 12:18:11
Original
1532 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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')
Copy after login

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})
Copy after login

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 class="title">{{ post.title }}</h2>
        <p class="date">{{ post.pub_date }}</p>
        <p class="content">{{ post.content }}</p>
    </div>
{% endfor %}
Copy after login

In the "myapp/detail.html" template, we will render the specific article :

<div class="post">
    <h2 class="title">{{ post.title }}</h2>
    <p class="date">{{ post.pub_date }}</p>
    <p class="content">{{ post.content }}</p>
</div>
Copy after login

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
Copy after login

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;
}
Copy after login

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);
    });
});
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template