Django start of the first day

Precautions

python interpreter recommend that you use between 3.4 and 3.7 to 3.6 do not use

django version of the problem 1.X 2.X

pip3 install django==1.11.11

django-admin startproject project name project name the file with the same name as the project folder _ the init _ .py settings.py urls.py wsgi.py manage.py Python manage.py the runserver

An empty django project is similar to a university app is equivalent to each college campus every college has its own corresponding function

python manage.py startapp application name (as far as possible with your echo function) created by the application must go settings.py file registration command line to create templates file is not, and has not settings.py file path configuration

 

pycharm create can automatically help you create the template folder and path configuration can also support the creation of an application and automatically register

Today contents of all resource users are able to access to the programmer in advance exposure good if not expose the user can not access forever

URL not write it will not be exposed

urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/', views.login), url(r'^reg/', views.reg), # url(r'^reg/', views.reg), # url(r'^home/', views.home), # url(r'^index/', views.index), ]

 

1. White will be three tricks

Import the render django.shortcuts from, HttpResponse, redirect HttpResponse # returns a string

return html page render #

 

Redirect redirect #

 

2. Static configuration file

Static files site used their own written js own written css third frame bootstrap fontwesome sweetalert static resource files usually used website are unified on the static folder STATIC_URL = '/ static /' # is static access Interface prefix resource "" "If you want to access static resources you have to start with static" ""

3. Manually configure a static file access resources

STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static'), os.path.join(BASE_DIR,'static1'), # os.path.join(BASE_DIR,'static2'), ]

Interface prefix dynamic analysis

htmt {% load static %} <link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}">

<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>

form form action parameters can be written in the form of 1. Do not write default towards current address submitted 2. Write only the suffix / index / 3. Write the full path

form the default form submission of a request toward the rear end by default get way get request carries parameter is behind url? url? username = admin & password = 213213213213213 drawback 1. unsafe parameters 2.get request carries a size limit (maximum no more than about 4KB)

If you want to submit preliminary request to post a comment out you go settings.py file middleware MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common .CommonMiddleware ', #' django.middleware.csrf.CsrfViewMiddleware ', ' django.contrib.auth.middleware.AuthenticationMiddleware ', ' django.contrib.messages.middleware.MessageMiddleware ', ' django.middleware.clickjacking.XFrameOptionsMiddleware ', ]

3.request Subjects and Methods

Front and rear ends data exchange 
how to obtain the requested mode

 

Data acquisition request carries a post 
  request.POST acquisition get request data carried   `` request.GET`` get and post at the rear when the user data acquired is the same law <QueryDict: { 'username': [ 'admin', 'tank'] , 'password': [ '123']}> Tank <class 'STR'> 123 <class 'STR'> request.POST.get ( 'username') a default last column of the list element just take the list if you want to you must remove the complete getlist ()
 







 

4.pycharm connect to the database django database connection

django connected MySQL Step configuration file is DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # specify the database postgreSQL MySQL 'NAME': 'day56', # in the end use to which the library 'the USER': 'root', 'PASSWORD': 'root', 'HOST': '127.0.0.1', 'PORT': 3306, 'CHARSET': 'utf8' } }

    The second step 
      django default is mysqldb connect to the database but the module does not support
      so you do not have to tell django with mysqldb the connection pymysql       you can also __init__ project name in the following __init__.py in the application name below. py file specified in the       Import pymysql       pymysql.install_as_MySQLdb ()
       


 

5.django orm Profile

orm object-relational mapping table class database records the object table in the object acquired attribute record a field value corresponding advantages: does not allow a person database operation can be simple and quick to use database disadvantages: Since the package high degree of efficiency in the implementation of the program could lead to lower sometimes combined with project requirements may require you to hand sql statement










Note orm 1.django will not automatically help you create a library, the library requires you to manually create table will automatically help you create you just need to write code to comply with django orm syntax

models.py is located next to the application in writing class

from django.db import models

Create your models here.

Userinfo class (models.Model): # set the id field as the primary key id int primary userinfo table AUTO_INCREMENT Key id = models.AutoField (primary_key = True) # in django you can not specify the primary key field django orm will automatically give you the current table called a new primary key field id # set the username field username varchar (64) CharField max_length parameters must be specified username = models.CharField (max_length = 64) # django but not char field exposed to the user can customize the char in django ORM field # int set password field password password = models.IntegerField () ** ** ** ** ** database migration (synchronous) command ** ** ** ** ** * Python does not create manage.py makemigrations # generate a table just record your current operation to record a small notebook (migrations folder)

python manage.py migrate # to migrate your real orm statement to (synchronized) to the database

As long as you modify the code in the database associated with models.py you have to restart the implementation of the above two commands

 

Guess you like

Origin www.cnblogs.com/whnbky/p/11716623.html