Home  >  Article  >  Backend Development  >  Quickly get started with the Django framework: detailed tutorials and examples

Quickly get started with the Django framework: detailed tutorials and examples

王林
王林Original
2023-09-28 15:05:10744browse

Quickly get started with the Django framework: detailed tutorials and examples

Quickly get started with the Django framework: detailed tutorials and examples

Introduction:
Django is an efficient and flexible Python Web development framework, developed by MTV (Model-Template -View) architecture driven. It has simple and clear syntax and powerful functions, which can help developers quickly build reliable and easy-to-maintain web applications. This article will introduce the use of Django in detail, and provide specific examples and code samples to help readers quickly get started with the Django framework.

1. Install Django
First, make sure the Python interpreter is installed. Then, you can install Django through the following command:

pip install django

After the installation is complete, you can use the following command to verify whether the installation is successful:

django-admin --version

2. Create a Django project
In the command line, pass The following command creates a Django project:

django-admin startproject mysite

This command will create a folder named mysite under the current folder to store all the files of the Django project.

3. Run the Django development server
Enter the mysite directory and execute the following command to start the development server:

cd mysite
python manage.py runserver

The development server runs at http://127.0.0.1:8000/ by default . Open this link in your browser to see Django's default welcome page.

4. Create a Django application
In Django, an application refers to a module with a specific function. Create a Django application through the following command:

python manage.py startapp myapp

This command will create a folder named myapp in the mysite directory to store all files of the Django application.

5. Write the model (Model)
Define the model (Model) in the models.py file in the myapp folder to describe the data structure of the application. The following is the code for a sample model:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    publication_date = models.DateField()

    def __str__(self):
        return self.title

The above code defines a model named Book, which contains fields such as title, author, and publication date.

6. Perform database migration
Execute the following command in the terminal to apply the model changes to the database:

python manage.py makemigrations
python manage.py migrate

The above command will automatically create a database table or update an existing table, to reflect the model definition.

7. Writing the view (View)
Define the view (View) in the views.py file in the myapp folder, which is used to process user requests and return corresponding results. Here is a simple view example:

from django.shortcuts import render
from .models import Book

def book_list(request):
    books = Book.objects.all()
    return render(request, 'book_list.html', {'books': books})

The above code defines a view called book_list, which gets all the books from the database and passes them to a template called book_list.html.

8. Writing Template (Template)
Create a folder named templates in the myapp folder, and create a file named book_list.html in it. Here is a simple template example:

{% for book in books %}
    <p>{{ book.title }} - {{ book.author }}</p>
{% endfor %}

The above code uses Django's template syntax to loop through the books on the page and display the title and author of each book.

9. Configure URL mapping
Configure URL mapping in the urls.py file in the mysite folder to route requests to the correct view. The following is an example:

from django.urls import path
from myapp.views import book_list

urlpatterns = [
    path('books/', book_list, name='book_list'),
]

The above code defines a URL mapping named book_list, which routes requests with the request path /books/ to the book_list view.

10. Run the Django development server
Restart the Django development server and visit http://127.0.0.1:8000/books/ in the browser to see the list of all books.

Conclusion:
This article introduces how to quickly get started with the Django framework, and provides detailed tutorials and examples. By installing Django, creating projects and applications, writing models, views, and templates, and configuring URL mapping, readers can quickly get started and start developing their own web applications. I hope this article can help readers understand and master the basic usage of the Django framework, and inspire readers to use their creativity to develop more powerful web applications.

The above is the detailed content of Quickly get started with the Django framework: detailed tutorials and examples. 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