Home > Backend Development > Python Tutorial > Getting Started Learning Django: A Complete Installation Guide

Getting Started Learning Django: A Complete Installation Guide

PHPz
Release: 2024-02-19 15:25:06
Original
732 people have browsed it

Getting Started Learning Django: A Complete Installation Guide

Learn Django from scratch: A comprehensive guide walks you through installation, specific code examples are required

Django is an open source web application framework based on Python that is widely used Build efficient, flexible and scalable websites and web applications. For beginners, mastering the basics of Django is the first step into the field of web development.

This article will help you learn Django from scratch. The comprehensive guide includes how to install Django and use specific code examples.

  1. Installing Python

Before you begin, first make sure you have Python installed. You can go to the official Python website https://www.python.org/downloads/ to download and install the latest version of Python.

  1. Installing Django

After installing Python, we can use Python's package management tool pip to install Django. Open a terminal or command prompt and enter the following command to install:

pip install django
Copy after login
  1. Create a Django project

After the installation is complete, we can start creating a Django project. In a terminal or command prompt, go to the directory where you want to create the project and run the following command:

django-admin startproject myproject
Copy after login

This will create a new folder named myproject in the current directory that contains the Django project basic structure.

  1. Run Django development server

After successfully creating the project, we can enter the project directory and run Django's development server. In a terminal or command prompt, go into the myproject directory and run the following command:

python manage.py runserver
Copy after login
Copy after login

This will start Django’s development server, which listens on http://localhost:8000/ by default. You can visit this address in your browser, and if everything is fine, you will see an initial Django welcome page.

  1. Creating a Django application

In Django, a project can contain multiple applications. An application is a small module in a project that implements a specific function. We can use Django commands to create a new application.

In a terminal or command prompt, go into the myproject directory and run the following command:

python manage.py startapp myapp
Copy after login

This will create a new folder called myapp under the myproject directory that contains the application basic structure.

  1. Define the model

In Django, models are used to define data structures. We can define models in the application's models.py file. Open the myapp/models.py file and define a model according to the following example:

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()
Copy after login

Here we define a model named MyModel, which contains two fields: name and age.

  1. Run database migration

After defining the model, we need to migrate the database to create the corresponding table structure. In a terminal or command prompt, go to the myproject directory and run the following command:

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

This will generate a database migration file based on the model changes and apply it to the database.

  1. Create a view

In Django, a view is a function or class that handles user requests. We can define views in the app’s views.py file. Open the myapp/views.py file and define a view according to the following example:

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

def my_view(request):
    return HttpResponse("Hello, Django!")
Copy after login

Here we define a view named my_view. When the user accesses the corresponding URL, a view containing "Hello, Django!" will be returned. "the response to.

  1. Configure URL mapping

Finally, we need to configure URL mapping to route user requests to the corresponding view. Add the following code to the application's urls.py file:

from django.urls import path
from . import views

urlpatterns = [
    path('my-view/', views.my_view, name='my-view'),
]
Copy after login

Here we map the my_view view to the URL of /my-view/.

  1. Run the Django development server

After completing the above steps, we can re-run the Django development server and access the URL we defined in the browser. In the terminal or command prompt, go to the myproject directory and run the following command:

python manage.py runserver
Copy after login
Copy after login

Then visit http://localhost:8000/my-view/ in the browser, you will see "Hello , Django!" response.

With the above steps, we have completed a comprehensive guide to learning Django from scratch. This guide walks you through the installation and basic usage of Django, and helps you understand better through specific code examples. I hope you can continue to learn and explore more functions and usage of Django, and build the web application you want!

The above is the detailed content of Getting Started Learning Django: A Complete Installation Guide. 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