Python's fifth-stage learning record---Django Foundation

Source: Internet
Author: User

Python's web framework has a variety of Django, Tornado, Flask and more, and Django has its advantages over other web frameworks: Chatty, the framework itself integrates ORM, model binding, template engine, caching, session and many more.

Basic Configuration

First, create a Django program

    • Terminal command: Django-admin startproject sitename
    • When the IDE creates a Django program, it is essentially automating the above command

Other common 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

Second, the program directory

Iii. Configuration Files

1. Database

1DATABASES = {2     'default': {3     'ENGINE':'Django.db.backends.mysql',4     'NAME':'dbname',5     'USER':'Root',6     'PASSWORD':'XXX',7     'HOST':"',8     'PORT':"',9     }Ten}

# 由于Django内部连接MySQL时使用的是MySQLdb模块,而python3中还无此模块,所以需要使用pymysql来代替

# 如下设置放置的与project同名的配置的 __init__.py文件中 import pymysql pymysql.install_as_MySQLdb() 

2. Template

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

3. static files

STATICFILES_DIRS =(

         os.path.join(BASE_DIR, ‘static‘ ),      )Routing System

1. Single Route Correspondence

URL (r'^index$', Views.index),

2. Regular-based routing

1 url (r'^index/(\d*)', Views.index),2 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 you set the name, you can call it in a different place, such as:

    • Template used to generate URL {% url ' h2 ' 2012}
    • The function uses the Generate URL reverse (' H2 ', args= (2012,)) path: Django.urls.reverse
    • Use the Get URL custom Get_absolute_url () method in model
classNewType (models. Model): Caption= Models. Charfield (max_length=16)    defGet_absolute_url (self):"""generate a URL app for each object: Generate a View-detailed URL in the object list and use this method!!! : Return:"""        #return '/%s/%s '% (self._meta.db_table, self.id)        #or         fromDjango.urlsImportReversereturnReverse'Newtype.detail', kwargs={'nid': Self.id})
View Code

Gets the URL information for the successful request match: Request.resolver_match

5, according to the app routing rules classification

url(r ‘^web/‘ ,include( ‘web.urls‘ )), 分发

6. Namespaces

A. project.urls.py

 fromDjango.conf.urlsImporturl,include urlpatterns=[url (r'^a/', Include ('App01.urls', namespace='Author-polls')), url (r'^b/', Include ('App01.urls', namespace='Publisher-polls')),]

B. app01.urls.py

 from Import URL  from Import  'app01'= [    url (r'^ (? p<pk>\d+)/$', Views.detail, name='detail')]

C. app01.views.py

def detail (Request, PK):     Print (request.resolver_match)     return HttpResponse (PK)

After you have defined the URL with the namespace, use name to generate the URL as follows:

    • v = reverse (' app01:detail ', kwargs={' PK ': 11})
    • {% url ' app01:detail ' pk=12 pp=99%}

The routing system in Django differs from the framework of other languages in that the URL for each request in Django has a route map so that the request can be handed to the function in the view for processing. Most other web frameworks are a route map for a class of URL requests, which makes the routing system concise.

Develop a dynamic routing system for Django through the Reflection mechanism demo: Download

Templates

The fifth stage of the Python learning record---Django basics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.