Home  >  Article  >  Backend Development  >  A Practical Guide to Django Development: Implementing Web Applications with Python and Django

A Practical Guide to Django Development: Implementing Web Applications with Python and Django

WBOY
WBOYOriginal
2023-06-22 23:20:20804browse

With the popularity of web applications, web development technology has also developed. As an efficient, simple, easy to learn, easy to use, and cross-platform programming language, Python is appreciated and adopted by more and more developers. As a highly modern web framework, Django provides developers with very convenient development tools and a powerful management system. This article will introduce how to use Python and Django to develop web applications, and provide beginners with a practical guide to using Django to develop web applications.

Part One: Environment Setup and Application Creation

First, you need to ensure that Python and Django are installed in the local environment. If it is not installed, you can download the corresponding installation package from the official website, and then install it through the command line or other methods.

After installing Python and Django, you can start creating a new application. Open a terminal and run the following command:

$ django-admin startproject myapp

This command will create a Django application named "myapp" in the current directory. Enter the "myapp" folder and run the following command:

$ python manage.py runserver

This command will start the Django development server and open a default page. If the page is opened successfully, it means that the environment setup and application creation are successful.

Part 2: Views and Routing

Next, you need to set up the view and establish routing. Create a new file named "views.py" under the "myapp" folder for writing view functions. For example, create a view function named "home":

from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to my app!")

Then, create a new file named "urls.py" under the "myapp" folder to specify routing. For example, define a route named "home" and point it to the "home" view function you just created:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

The path in this route file is empty, which means that as long as you access the application's root directory, the "home" view function will be called.

Part 3: Model and Database

Now you need to create a model and save it in the database. Create a new file named "models.py" under the "myapp" folder.

For example, create a model named "User", including two fields: "Username" and "Password":

from django.db import models

class User(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=20)

    def __str__(self):
        return self.username

After the model is defined, you can create it by running the following command Database:

$ python manage.py makemigrations
$ python manage.py migrate

After running the above command, Django will automatically create a database file named "db.sqlite3", And save the model just defined in it.

Part 4: Template

Now you need to create an HTML template for displaying the content of the application in the browser. Create a new folder named "templates" under the "myapp" folder, and then create a new HTML template file named "home.html" in it.

For example, create an HTML template that contains a simple table:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Welcome to my app!</h1>
    <table>
        <tr>
            <th>Username</th>
            <th>Password</th>
        </tr>
        {% for user in users %}
        <tr>
            <td>{{ user.username }}</td>
            <td>{{ user.password }}</td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>

In this template, loop through to output the username and password information for all users saved in the database.

Part 5: Connection between view and template

Now you need to connect the view and template. Modify the "home" view function in the "views.py" file so that it obtains user information from the database and passes that information into the HTML template.

from django.shortcuts import render
from .models import User

def home(request):
    users = User.objects.order_by('username')
    return render(request, 'home.html', {'users': users})

In this modified view function, use the "User.objects.order_by('username')" statement to select all users from the database and sort them alphabetically by username. This user information is then passed into HTML templates so that they can be displayed in the browser.

Part Six: Front-end Design Beautification

The last part is to beautify the front-end design to make the application look more beautiful and comfortable. This can be achieved by installing UI frameworks such as Bootstrap.

For example, you can install Bootstrap and use it in an HTML template:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1>Welcome to my app!</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>Username</th>
                    <th>Password</th>
                </tr>
            </thead>
            <tbody>
                {% for user in users %}
                <tr>
                    <td>{{ user.username }}</td>
                    <td>{{ user.password }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</body>
</html>

In this modified HTML template, Bootstrap’s style and layout are used to make the page look fresher. More design sense.

Summary

This article introduces how to develop web applications using Python and Django in six steps. In this process, it covers everything from environment construction and application creation, to view and routing settings, to models, databases, templates, and front-end design and beautification. I believe this article can provide beginners with a simple and easy-to-understand practical guide to developing web applications using Django to help them get started quickly.

The above is the detailed content of A Practical Guide to Django Development: Implementing Web Applications with Python and Django. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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