Home  >  Article  >  Backend Development  >  Getting Started: A Complete Guide to Correctly Installing and Configuring the Django Framework

Getting Started: A Complete Guide to Correctly Installing and Configuring the Django Framework

WBOY
WBOYOriginal
2024-02-19 23:42:07411browse

Getting Started: A Complete Guide to Correctly Installing and Configuring the Django Framework

Django Getting Started Guide: How to properly install and set up the Django framework

Introduction:
Django is a powerful Python web framework that provides a complete set of solution to facilitate developers to quickly build efficient web applications. This article will take you step by step to understand how to correctly install and set up the Django framework, including installing related software, creating projects and applications, and providing specific code examples. I hope this article can help you get started with the Django framework quickly.

Step One: Install Python and pip
Before you start installing Django, you first need to install Python and pip, because Django is based on Python. You can download and install the latest version of Python from the official Python website and make sure to add it to the system environment variables. After the installation is complete, you can enter the following command in the terminal to check whether Python and pip are installed successfully:

python --version
pip --version

Step 2: Install Django
After installing Python and pip, you can use the pip command to install Django . Enter the following command in the terminal:

pip install django

This will install the latest version of the Django framework.

Step 3: Create a Django project
After installing Django, you need to create a Django project. Enter the following command in the terminal:

django-admin startproject myproject

This will create a folder named myproject, which contains the basic structure of the project. The name of the project can be modified according to the actual needs of the project.

Step 4: Run the Django development server
After the project is created, you need to enter the project folder and run the development server. Enter the following command in the terminal:

cd myproject
python manage.py runserver

This will start Django’s development server, which by default runs on port 8000 of your localhost. You can visit http://localhost:8000/ in a web browser to view the running results. If all goes well, you will see a default Django welcome page.

Step 5: Create a Django application
In Django, a project consists of one or more applications. A Django app can be created with the following command:

python manage.py startapp myapp

This will create a folder called myapp in the project folder, which contains the basic structure of the application. The name of the application can be modified based on the functionality and needs of the application.

Step 6: Configure the application
After creating the application, you also need to configure it into the Django project. Open the myproject/settings.py file, find the INSTALLED_APPS list, and add your application to it:

INSTALLED_APPS = [
    ...
    'myapp',
]

Step 7: Create a database
If your application needs to use a database, you need to create it first Database and configure Django connection. Find the DATABASES settings in the myproject/settings.py file and make corresponding settings according to the type and configuration of the database. For example, for a SQLite database, you can use the following configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Step 8: Create a model
In Django, a model is used to represent the table structure in the database. Models can be defined in the myapp/models.py file. For example, the following code defines a simple model:

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=50)
    age = models.IntegerField()

Step 9: Migrate the database
After defining the model, you need to synchronize the database structure through migration. Enter the following command in the terminal:

python manage.py makemigrations
python manage.py migrate

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

Step 10: Write view and URL configuration
View and URL configuration are one of the core components of Django. They are used to process HTTP requests and generate corresponding responses. Write the view function in the myapp/views.py file, for example:

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

def index(request):
    return HttpResponse("Hello, Django!")

Then, open the myproject/urls.py file, add URL configuration, and map the view function to the URL:

from django.urls import path
from myapp import views

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

Step 11: Run the Django development server
After configuring the view and URL, re-run the Django development server:

python manage.py runserver

You can view it by visiting http://localhost:8000/ in a web browser Updated results.

Summary:
This article briefly introduces how to correctly install and set up the Django framework, and provides detailed code examples. I hope that through this article, you can have a preliminary understanding of how to use Django, and be able to successfully create and deploy your own Django project. In actual development, there are many more in-depth contents and techniques waiting for you to explore. I wish you good results using the Django framework!

The above is the detailed content of Getting Started: A Complete Guide to Correctly Installing and Configuring the Django Framework. 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