Introduction to Python

Python is an interpreted, object-oriented, dynamic data typed high-level programming language.

Python was invented by Guido van Rossum in late 1989, with the first public release in 1991.

Like Perl, Python source code is governed by the GNU General Public License (GPL).

Second, the use of

I’m using a new version of Python3, which is said to be quite different from the previous version of Python2

www.runoob.com/python3/pyt… It shouldn’t be difficult to get started with Python if you know Java.

Python Web Framework

Django is an open source Web application framework written in Python. MVC framework pattern is adopted, that is, model M, view V and controller C. It was originally developed as CMS (Content Management System) software to manage some of Lawrence publishing group’s news-focused websites. It was released under the BSD license in July 2005. The frame is named after Belgian gypsy jazz guitarist Django Reinhardt.

Flask is a lightweight Web application framework written in Python. The WSGI toolkit uses Werkzeug and the template engine uses Jinja2. Flask uses BSD authorization. Flask is also known as a “microframework” because it uses a simple core and adds additional functionality with Extension. Flask has no default database, form validation tools.

Cherry Py is an object-oriented HTTP framework based on Python, suitable for Python developers. Use Cherry Py to develop Web applications, but Cherry Py does not provide a template language mechanism. Cherry Py has its own built-in Web server. Cherry Py users do not need to set up a separate Web server and can run applications directly on the built-in server. The server is responsible for: on the one hand, it converts the information transmitted by the underlying TCP socket into Http request, and transmits it to the corresponding handler; On the other hand, the information from the upper software is packaged as AN Http response and passed down to the underlying TCP socket. .

Install the Django environment

PIP command installation

pip install Django
Copy the code

Enter after the installation is complete

django-admin
Copy the code

This means the installation is complete

PyCharm is recommended for editor. I use idea directly here, but I need to install a Python plug-in and install the method

File->Settings->Plugins  
Copy the code

Search the Python

5. Project creation

Basic commands:

Creating a Django project

django-admin.py startproject xxxx
Copy the code

Creating a Django application

python manage.py startapp xxxx
Copy the code

Starting the Django project

python manage.py runserver 8080
Copy the code

Synchronize changes to database tables or fields

python manage.py makemigrations
python manage.py migrate
Copy the code

Clearing the database

python manage.py flush
Copy the code

Creating a Super Administrator

python manage.py createsuperuser    The email can be left blank. The username and password are mandatory

 
python manage.py changepassword username  # Change the user password
Copy the code

Django project environment terminal

python manage.py shell
Copy the code

Get down to business, go to your own folder, initialize a Django project, and open it using the editor

django-admin.py startproject django_demo
Copy the code

The project is listed below

At this point we can start the project to have a look, directly in the editor terminal input command can be

Visit to see the effect

Such a Django project has already been created

6. Application creation and route allocation

In fact, I’ve already created an application called “blog”, just create it using the create command, and create a new urls.py routing file under the blog file

1. Write views.py under the blog application

from django.http import HttpResponse

def hello_word(request):
    return HttpResponse("hello word")
Copy the code

2. Write urls.py for your blog application

import blog.views

urlpatterns = [
    path('hello_word', blog.views.hello_word)
]
Copy the code

3. Write urls.py in the project file

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')))Copy the code

4. Take a test