Home > Backend Development > Python Tutorial > Use Django to quickly build personal applications

Use Django to quickly build personal applications

WBOY
Release: 2024-02-26 12:09:09
Original
1253 people have browsed it

Use Django to quickly build personal applications

Easily create your own applications with Django

Django is a Python framework for developing efficient web applications. It provides rich features and easy-to-use tools, allowing developers to quickly build powerful applications. This article explains how to create your own application using Django and provides some concrete code examples.

  1. Installing Django

First, you need to install Django on your machine. You can use the pip tool that comes with Python to install it. Open a terminal or command prompt and enter the following command:

pip install django
Copy after login

This will automatically download and install the latest version of Django.

  1. Create Django project

After installing Django, we can create a new Django project. Execute the following command on the terminal:

django-admin startproject myproject
Copy after login

This will create a directory called myproject and generate the initial Django project structure.

  1. Create a Django application

Enter the myproject directory and execute the following command to create a new Django application:

python manage.py startapp myapp
Copy after login

This will create a new Django application named directory for myapp and generate the initial Django application structure.

  1. Configure Django project

Next, we need to configure the Django project. Open the myproject/myproject/settings.py file and modify some parameters in it:

INSTALLED_APPS = [
    # ...
    'myapp',
    # ...
]

# ...

LANGUAGE_CODE = 'zh-hans'

# ...

TIME_ZONE = 'Asia/Shanghai'
Copy after login

Add 'myapp' in the INSTALLED_APPS list to add our application to the Django project. Modify LANGUAGE_CODE to 'zh-hans' to set the default language to Simplified Chinese. Modify TIME_ZONE to 'Asia/Shanghai' to set the default time zone to Shanghai.

  1. Create a model

Model is one of the core concepts in Django, which is used to define the structure of data. Create a file named models.py in the myapp directory and define the model in it:

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
Copy after login

The above code defines an Article model, which contains the title, content and creation time of the article.

  1. Perform database migration

Django uses migration tools to manage database schema changes. Execute the following command on the terminal to create and apply the migration of the model:

python manage.py makemigrations
python manage.py migrate
Copy after login

This will automatically generate a migration file and apply the model to the database.

  1. Create View

View is responsible for processing HTTP requests and returning responses. Create a file named views.py in the myapp directory and define the view in it:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Article

def index(request):
    articles = Article.objects.all()
    return render(request, 'index.html', {'articles': articles})
Copy after login

The above code defines a view function named index, queries all articles, and renders the index through the render function .html template.

  1. Create a template

The template is used to render the page. Create a directory named templates in the myapp directory and create a template file named index.html in it:

<!DOCTYPE html>
<html>
<head>
    <title>My Django App</title>
</head>
<body>
    <ul>
    {% for article in articles %}
        <li>{{ article.title }}</li>
    {% endfor %}
    </ul>
</body>
</html>
Copy after login

The above code defines an HTML page with a title and displays it through the template variable All article titles.

  1. Configure URL routing

URL routing is used to map requests to the corresponding views. Open the myproject/myproject/urls.py file and modify the code in it:

from django.urls import include, path
from myapp import views

urlpatterns = [
    path('', views.index, name='index'),
]
Copy after login

The above code maps the root URL to the index view.

  1. Running Django Server

Run the following command to start the Django development server:

python manage.py runserver
Copy after login

Now, you can visit http://localhost in your browser :8000/ to see the titles of all articles displayed on the page.

Through the above steps, we successfully created a simple Django application and implemented the display of articles. Of course, this is only a small part of Django's capabilities, and there are more powerful features that can help us build more complex applications. I hope this article will help you understand and use Django!

The above is the detailed content of Use Django to quickly build personal applications. For more information, please follow other related articles on the PHP Chinese website!

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