python learning diary --django basis

Zero environmental structures

1.python3 installation
# Wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
#tar -zxvf the Python-3.6.6.tgz
#cd the Python-3.6.6
#. / configure --prefix = install path
# make && make install
installation of 2.pip3
yum the install to python3-PIP
3.Django frame mounting
PIP3 the install Django
4.sqlite upgrade

First, the basic

1. Create a django project (ie download / deploy web framework code)
command: django-admin startproject mysite
generated named mysite project
directory structure
mysite
  mysite
    settings.py //
    urls.py //
    wsgi.py //
  manage.py
2 by manage.py file management project
command: python manage.py startapp blog
created a blog called application
directory structure
mysite
  blog
    admin.py // backstage management system provided by django
    generation app name apps.py // django of documents
    models.py // database operation using
    tests.py // single measured using
    views.py // store a variety of functions
  the mysite
    the settings.py // Django configuration items (applications, databases, etc.)
    the urls.py // URL distributed
    wsgi. py // the request (or response) encapsulated by a specific rule
  manage.py
  templates // store template file, the data acquired from echo views and
  static // store css, js, img and other static files

Special episode: os.path.join (path, file) - Returns the file path based on different operating systems mosaic of
tuples need to look at the comma problem
STATIC_URL = '/ static /' // static file path alias, follow here path to find the file
STATICFILES_DIRS = (// root directory of static files to configure
  os.path.join (base_dir, "static"),
)
3.render (rendering) method --template.render (Context ({ "key1" : value1 , "key2": value2}))
return the render (request object, the template file name, data)
DEF Demo (request):
  value1 = "my values are passed on the page"
  value2 = "I need the key value of the page referenced in place corresponding to "
  return the render (Request," index.html ", {" key1 ": VALUE1," key2 ": value2})
special episode: template data into <h1> {{key1}} </ h1>
introducing the template file <Script the src = "{% static 'jQuery-3.1.1.js'}%"> </ Script>
return the render (Request, "index.html",local ()) - At this template you can use the values of variables in the function
return redirect ( "/ login /" ) - routing redirection will change the URL in your browser address bar, re-take the new route rendering loading process
4.url control
the urlpatterns = [
  path (regular, functions, aliases),
]
URL packet: small brackets path parameters, parameters are passed in order to function
without naming groups:
previous version 2.0
path ( 'ADMIN / (\ d {4}) $ ', view.fun)
release after 2.0
path (' Articles / <int: year> / <path: path> / <STR: STR> / ', views.fun),
DEF Fun (Request , year) {...} // year named herein may optionally replaced
with a group name:
before 2.0
? path (? 'admin / ( P <year> \ d {4}) / (P <month > \ d {2}) ' , view.fun)
release after 2.0
re_path (' Articles / (? P <year> [0-9] {}. 4) / (? P <month the> [0-9] { } 2) / (? P <Slug> [\ W -_] +) / ', views.article_detail),
DEF Fun (Request, year, month) {...} // year and month herein named immutable
alias: absolute path by rendering {% url 'alias' mode}%

Distribution url: url ( "blog /", include ( 'blog.urls')) - at the beginning of a path blog urls need to enter the blog document to match the application (first match blog, back into the path to match blog.urls , such as: blog / article in, blog URLs file directory need to configure the url ( "article", views.article))

Two, model (database-related)

 

 

 

 

Guess you like

Origin www.cnblogs.com/ftxy/p/12007104.html