This article is participating in Python Theme Month. See the link to the event for more details

Rest cannot be enjoyed by lazy people.~

preface

This article covers the basics of using The Django framework, one of python’s three major Web frameworks. How to create a Django project, how to run a Django project, the directory structure of a Django project, and how Django returns different data and pages.

Three major Python Web frameworks

There are three major web frameworks for Python: Django, Flask, and Tornado. The table below briefly compares the three frameworks:

Django flask tornado
Fully functional short Asynchronous non-blocking, supports high concurrency
Sometimes too heavy Rather rely on third-party modules .

Django installation

Django comes in 1.x, 2.x, and 3.x versions, and each version is different. If you don’t specify a version, install the latest version by default. Verify the installation by running Django-admin on the command line.

pip install django  Install the latest version
pip install django==1.11  # install the specified version, version 1.11
Copy the code

Basic Django Operations

The basic Django operations include creating a Django project, starting a Django project, and creating an application, using the command line and PyCharm, respectively.

Command line operation

The first step is to create a Django project. When you create a Django project from the command line, you don’t need to open the Python interpreter. You can do it directly from the command line.

django-admin startproject first_django
Copy the code

After creating the project folder first_django, the file structure is as follows:

After successfully creating your project, you can run your Django project. To run your Django project, switch to the directory where manage.py is located and run the following command:

python manage.py runserver
Copy the code

Another command is to create an application. Here we need to explain the meaning of an application – an application is simply a separate functional module, such as a mall project can be divided into order application, user application, commodity application, etc. To create an application, you also need to switch the directory to the path where manage.py is stored.

# The name of the application is based on the name
# Python manage.py startApp name
python manage.py startapp first
Copy the code

To highlight! Underline!! Every time you create an application, you register it in the settings.py file.

# settings.py code for the registration application section
INSTALLED_APPS = [
    'django.contrib.admin'.'django.contrib.auth'.'django.contrib.contenttypes'.'django.contrib.sessions'.'django.contrib.messages'.'django.contrib.staticfiles'.'first.apps.FirstConfig'.# Write all, recommended way
    'first'.# shorthand
]
Copy the code

Pycharm operation

The PyCharm Community edition does not support creating Django projects directly, only the professional edition does. However, both the professional and community versions can run Django projects. Professional PyCharm simply selects Django when creating a Django project.

Pycharm can run commands directly from pyCharm terminals when running Django projects and creating applications:

python manage.py runserver  Run the Django project
python manage.py startapp first  # Create application
Copy the code

The difference between the command line and PyCharm in creating Django projects

There is a slight difference between creating a Django project on the command line and creating a Django project on pyCharm. Instead of creating a template folder on the command line, you need to create it manually. Pycharm does that for you and automatically sets the path in the configuration file. This means that not only do you need to manually create the Templates folder, but you also need to configure the path in the configuration file when creating a Django project with a command.

# PyCharm creates -settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates'.'DIRS': [os.path.join(BASE_DIR, 'templates')],]# Command line creation -settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates'.'DIRS': []]Copy the code

Introduction to Main Documents

Introduction to the directory structure and files after creating the Django project and creating the application.

The django framework withCustomize the Web framework

While the Python Web framework implements splitting files and returning different data or pages based on different urls, the Django framework naturally implements these two “little” functions. In the Django project views.py, the functions that are wrapped in the derivation of the Web framework are called view functions. Urls.py contains the mapping between view functions and routes. How does Django return data to the client? You need to use three methods – HttpResponse, render, redirect, according to the actual code demonstration.

HttpResponse, render, redirect basic use

# HttpResponse - Return string data when defining a view function
# views.py
def index(request) :
    ":param request: requests all data objects associated with the request, more than the previous env, which is described later in the request object :return:HttpResponse (returns a string to the browser client)"
    return HttpResponse('welcome my django! ')
    
# urls.py
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', views. The index),] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --# render - returns the HTML page to the browser client when defining the view function
def index(request) :
    "" :param request: requests all related data objects, more than the previous env, 
      
        :return:render(request,'render_html.html'): return:render(request,'render_html.html'): return:render(request,'render_html.html')
      
    return render(request,'render_html.html') -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --# redirect - When you define a view function, the return value will redirect the page
def index(request) :
    ":param request: requests all data objects related to the redirect, which is more than the previous env :return:redirect, which redirects a web page, either to a url or to a route in your own project (just write the url suffix)."
    # return redirect('https://www.baidu.com'
    return redirect('/home/')   Redirect to the /home page in the current project

def home(request) :
    return HttpResponse('I'm a custom')
Copy the code

conclusion

The article was first published in the wechat public account program Yuan Xiaozhuang, synchronized with nuggets.

Please explain where it came from. If you pass by, please put out your cute little finger and click like before you go (╹▽╹)