Home  >  Article  >  Backend Development  >  Guide: Detailed steps teach you how to create a Django project using commands

Guide: Detailed steps teach you how to create a Django project using commands

PHPz
PHPzOriginal
2024-02-19 08:29:06638browse

Guide: Detailed steps teach you how to create a Django project using commands

Django Project Creation Guide: Teach you step by step how to create a project using commands, specific code examples are required

Introduction:
Django is a powerful development framework. It helps developers quickly build high-quality web applications. This article will introduce in detail how to use Django commands to create a new project and give specific code examples.

1. Install Django

Before starting to create a Django project, we first need to install Django on the computer. You can install the latest version of Django in the terminal through the following command:

pip install Django

2. Create a project

  1. Open the command line interface and enter the directory where you want to create the project.
  2. Create a new Django project using the following command:
django-admin startproject myproject

This will create a folder called "myproject" in the current directory and generate a Django project in it basic structure.

3. View the project structure

After the project is successfully created, you can view the directory structure of the project through the following command:

cd myproject
ls

After running the above command, you will see something like Based on the following directory structure:

manage.py
myproject/
    __init__.py
    settings.py
    urls.py
    wsgi.py
  • manage.py: A command line utility for executing various Django commands.
  • myproject/: This folder is the main directory of the project and contains files and sub-applications related to project settings.
  • __init__.py: An empty file that tells Python that the directory is a Python package.
  • settings.py: Contains project settings and configuration, such as database connection, static file path, etc.
  • urls.py: Define the URL routing rules of the project.
  • wsgi.py: An entry point for deploying your project to a WSGI-compatible server.

4. Run the project

  1. Use the following command to enter the project directory:
cd myproject
  1. Run the following command to start the Django development server:
python manage.py runserver

After running successfully, you will see output similar to the following:

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
  1. Enter http://127.0.0.1:8000/ in the browser, You will see Django’s default welcome page.

5. Create an application

The Django application is a component of the project and can be regarded as a sub-module of the project. The following will demonstrate how to create an application named "blog":

  1. Use the following command to create a new application in the project directory:
python manage.py startapp blog

This will Create a folder named "blog" in the project directory, which contains the basic structure of the application.

  1. Add the newly created application to the INSTALLED_APPS list in the myproject/settings.py file:
INSTALLED_APPS = [
    ...
    'blog',
]

Six , Writing views

Django’s views process user requests and return corresponding functions. A simple view example will be shown below:

  1. Write the following view in the blog/views.py file:
from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, Django!")
  1. In blog/urls.pyAdd URL routing rules to the file:
from django.urls import path
from . import views

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

7. Run the application

  1. Run the following command to start the Django development server:
python manage.py runserver
  1. Enter http://127.0.0.1:8000/blog/hello in the browser, you will see the "Hello, Django!" page.

Conclusion:
This article details the process of using Django commands to create a new project, including installing Django, creating the project, viewing the project structure, running the project, creating applications and writing views etc. content. I hope this article can help you get started with Django development quickly. Happy programming!

The above is the detailed content of Guide: Detailed steps teach you how to create a Django project using commands. 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