Django study notes (1) --- Hello Django

Life is too short~ 

Tips: Only applicable to Python 3+ (not much difference anyway, py2 can also be changed). Because according to the father of Python, Guido van Rossum, the official support for Python 2 will be stopped in 2020, so if you are still using Python 2, you should prepare early. After all, it is not satisfactory to use without official support.

 

1. Prepare the Python and Django environments

Don't have a Python environment yet? Python download address:  https://www.python.org/downloads/

For Django installation, please see the Django basic construction service of Python web framework here 

 

2. Start creating our project

django-admin.py startproject HelloDjango

The following directory structure will appear next:

HelloDjango
 ---- HelloDjango # Container for the project
     ---- __init__.py # Empty file, telling Python that this directory is a Python package
     ---- settings.py # Settings/configuration for this Django project
     ---- urls. py # The URL declaration of the Django project; a "directory" of the website powered by Django
     ---- wsgi.py # The entry point for a WSGI-compatible web server to run your project
 ----manage.py # Commands line tool to interact with this Django project

 

3. Start the server

Enter our project HelloDjango:

python manage.py runserver # use port 8000 by default

Enter the ip and port number of your server in the browser. If it starts normally, the output is as follows:

 

Tips: If you do not output Hello Django! in the APP application, complete the relevant output operations above.

 

4. Create a Django app

Enter our project HelloDjango:

python manage.py startapp mydjango

So the mydjango folder appeared again:

HelloDjango
 ---- HelloDjango
 ---- manage.py
 ---- db.sqlite3 # sqlite3 database is used by default
 ---- mydjango # app application directory, equivalent to a submodule of the project
     ---- migrations
         --- - __init__.py
     ---- __init__.py
     ---- admin.py # Register your module with Django, it will create Django's admin interface
     ---- apps.py
     ---- models.py # Store the The place where the data module is applied, where the entities and relationships of the data are described
     ---- tests.py # Store the test code of the application
 ---- views.py     # Process application requests and responses

views.py and models.py are used by every application and are the Django design pattern MVT (Model-View-Template).

 

5. Associate the mydjango APP with the HelloDjango project

Whenever you have a new application, you need to add the application name to INSTALLED_APPS in the project's configuration file setting.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mydjango',
]

 

6. Create the view

Open the newly created app mydjango directory, find the file views.py and open it and add the following code:

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

def hello(request):
    return HttpResponse("Hello Django!")

 

7. URL mapping access

Create a file urls.py in the current application directory and map the URLs that need to be accessed:

from django.urls import path
from mydjango import views

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

 

8. Associate the URL in the project with the URL in the APP application

Open the HelloDjango folder to find the urls.py file, open and add the following code:

from django.contrib import admin
from django.conf.urls import url, include

urlpatterns = [
    url('admin/', admin.site.urls),
    url('mydjango/', include('mydjango.urls')),
]

When accessing the mydjango URL, it will be mapped to the urls.py file in the mydjango APP application, and then enter the mapped URL in the file to access, so enter:

http://127.0.0.1:8000/mydjango/hello/

result:

 

~ I learn Python 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325213154&siteId=291194637