Django Basics [-1]

P has a variety of Django, Tornado, Flask and other ython the WEB framework, Django compared to other WEB framework of its advantages: large and framework itself integrates the ORM, model binding, template engine, cache, Session, and many other functions.

basic configuration

First, create a program django

  • Terminal command: django-admin startproject sitename
  • When Django IDE create the program, these commands are executed automatically on nature

Other commonly used commands:

  python manage.py runserver 0.0.0.0
  python manage.py startapp appname
  python manage.py syncdb
  python manage.py makemigrations
  python manage.py migrate

  python manage.py createsuperuser

 

Third, the configuration file

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME':'dbname',
    'USER': 'root',
    'PASSWORD': 'xxx',
    'HOST': '',
    'PORT': '',
    }
}
# The use of the internal module MySQLdb Django MySQL is connected, and also no such python3 module, it is necessary to replace the use of pymysql 
  
# set as follows __init__.py project file configured with the same name is placed in 
  
Import pymysql 
pymysql.install_as_MySQLdb ( )
View Code

2, stencil

TEMPLATE_DIRS = (
        os.path.join(BASE_DIR,'templates'),
    )

3, static files

STATICFILES_DIRS = (
        os.path.join(BASE_DIR,'static'),
    )

Routing System

1, corresponding to a single route

url(r'^index$', views.index),

2, based on regular routes

url(r'^index/(\d*)', views.index),
url(r'^manage/(?P<name>\w*)/(?P<id>\d*)', views.manage),

3, add additional parameters

url(r'^manage/(?P<name>\w*)', views.manage,{'id':333}),

4, set the name for the route map

url(r'^home', views.home, name='h1'),
url(r'^index/(\d*)', views.index, name='h2'),

After setting the name, you can call in different places, such as:

  • Using the template generated URL {% url 'h2' 2012%}
  • Function using the generated URL reverse ( 'h2', args = (2012,)) path: django.urls.reverse
  • Model used to obtain a custom URL get_absolute_url () method
class of NewType (models.Model): 
    Caption = models.CharField (MAX_LENGTH = 16 ) 


    DEF a get_absolute_url (Self):
         "" " 
        generate a URL for each object 
        application: see detailed URL generated in the object list, i.e., the use of this method can !!! : 
        return: 
        "" " 
        # return '/ S% / S%'% (self._meta.db_table, self.id) 
        # or 
        from django.urls Import Reverse
         return Reverse ( ' NewType.Detail ' , kwargs {= ' NID ' : self.id})

5, according to app to classify the routing rules

url(r'^web/',include('web.urls')),

 

 

 

   

2, stencil

1
2
3
TEMPLATE_DIRS = (
         os.path.join(BASE_DIR, 'templates' ),
     )

3, static files

1
2
3
STATICFILES_DIRS = (
         os.path.join(BASE_DIR, 'static' ),
     )

Guess you like

Origin www.cnblogs.com/fuyuteng/p/12216006.html