Python Web Development Module Django2.2 study notes -1 (Django process of making web pages briefly: (only for windows))

  1. Create a virtual environment/python -m venv <venv_name>
  2. Activating virtual environment/<venvname>/Scripts/activate
  3. Create a project/django-admin.py startproject <project_name>
  4. Create a database/python manage.py migrate
  5. Run the server/python manage.py migrate
  6. Creating App / Python manage.py <app_name>
  7. Adding superuser/python manage.py createsuperuer
  8. Definition model:
    Open a folder <app_name> models.py file, create a model (i.e., the definition of a class)
  9. Activation model:
    Open <project_name> /setting.py, the <app_name> added to the INSTALLED_APPS
  10. Register Model
    Open <app_name> /admin.py, introduced into the model, and register the model
  11. Migration model /python manage.py makemigrations, /python manage.py migrate
    due model.py Edit, create church Django how to modify the migration file database, and then apply the migration
  12. Configuration URL:
    Open <project_name> /urls.py, introduction <app_name> of views (app automatically generated when creating the document), increasing url mode
    two kinds of modes url structure currently understood:
    A mode to increase a url:
    path('', views.<view_function_name>[, name='<urlpattern_name>'])
    ''Representative is requested to the part after the body of the page, for example, the embodiment refers to http: // localhost: 8000, if the ''replace 'about/'said page request http: // localhost:. 8000 / about /
    viewsis a file <app_name > /views.py, view_function_namerefer to the referenced return web content view function, which will edit the subsequent steps
    name='<urlpattern_name>'(optional) refers to this url model name, so that we can refer to it elsewhere in the code. Whenever the need to provide a link to the home page, we will use this name, URL and not write
    two adding a set of url patterns:
    path('', include('<other urlpattern>'[, namespace='<namespace_name>']))
    If the <app_name> /urls.py create a new extra_urlpatterns in the current file, then the 'other urlpattern'Replace 'extra_urlpatterns', which can be combined so that urlpatterns view url handler
    reference: https://docs.djangoproject.com/zh-hans/2.2/topics/http/urls/
    namespace='<namespace_name>' (optional) means able to let the URL learning_logs other projects in the URL area with separate, which is helpful when the project began to expand.
    Reference: "Python Programming: from entry to practice" on page 193
  13. Write view function:
    Open the <project_name> /views.py, write the function
    currently two ways to understand
    a very simple if website content, import HttpResponse, to convert a string of HTML code into the HTML code.
    from django.http import HttpResponse
    ...
    return HttpResponse("<h1>Hello World!</h1>")
    two complex web content, will be certain. html file as a parameter is added to the render (request, '<html file>') function
    return render(request, 'learning_logs/index.html')
  14. Authoring templates (if used during the preparation of view function is a simple web method, naturally without template):
    two ways:
    A no inheritance:
    the direct line and the code it wants
    two inherited from the parent template:
    Note the parent and child template formatting template
    reference: "Python programming: from entry to practice" & 194 193

Guess you like

Origin blog.csdn.net/weixin_43243428/article/details/91411646