What is Django?
- Django is a python-based advanced web development framework of
- It allows developers to be efficient and rapid development
- The highly integrated (do not have to create the wheel), free and open source
The current path creation project
django-admin startproject myblog
- Directory name not propose to amend
- Wsgi.py python server gateway interface (an interface between the python with the web server application)
- Urls.py Django project, all addresses (pages) will need to configure their own URL
- Settings.py configuration database, etc.
- __init_ modularity, may be introduced
Create an application :
- Open a command line, enter the project in the same directory manage.py
- Command line, type: python manage.py startapp blog
- Settings.py to add the application name in the Insalled_APPS
- migrations data migration (migration) module
- admin.py: the back-end application configuration management system (Django's own back-end management system)
- apps.py: some configurations currently applied automatically in the future Django-1.9
- models.py: a data module, using ORM framework structure similar to Models MVC (Model).
- views.py: execute the corresponding code module where the main location code logic processing, the project most of the code that are written here
Code begins
The second project URl configuration
- Include the introduction of the root urls.py
- Urls.py file is created in the App Catalog, the same format as the root urls.py
- Url urls.py the root function of the second parameter to include ( 'blog.urls')
- Urls.py root URL for the configuration of App name, App is the total of all the URL's path
- When configuring url regular expression and the end symbol $ /
Templates Description:
- Templates file is stored HTMl
- Use the Django template language (Django Template Language, DTL)
- You can use a third-party templates (such as jinja2)
Where modifications:
- TEMPLATES Setting the value of the BACKEND
Development of a Template
- Create a directory called Templates in the root directory of the app
- Create html file in the directory
- Rendering to the browser render the views inside
return render(request, "index.html")
- Render () third argument to support a dict type parameter
- The dictionary is a backstage pass to the parameter template, the key is the parameter name
- Used in the template parameter name {} {} used directly
Django Find Template
- Finding Templates Django added in order INSTALLED_APP
- Templates directory of the same name .html file will cause conflict in different APP
TemplatesTemplates resolve conflict programs
- Create a directory called APP name in the Templates directory of APP
- Html files into the new directory under
Models Introduction
- Typically, a Model database corresponding to a data table
- In Django Models shown in the form of class
- Contains some basic fields as well as some of the acts of data
SNAKE
- Object-relational mapping (Object Relation Mapping)
- Achieve the mapping between objects and databases
- Hides the details of data access, does not need to write SQL statements
Write Models steps:
- Models.py created in the root directory of the application, and the introduction of models module
- Create a class that inherits models.Model, means one type of data tables
- Create fields in the class
Fields are created:
- I.e., properties of the class field (variable)
- Attr = models.CharField(max_length=64)
And other parameters can be introduced here
https://docs.djangoproject.com/en/1.10/ref/models/fields/
Generating a data table the steps of:
- Manage.py command line to enter the directory statistics
- Execute python manage.py makemigrations app name (optional)
- In the execution python manage.py migrate
View:
- Django will automatically generate the migration file in app / migrations / directory
- Execute python manage.py sqlmigrate application file name id see the SQL statement (python manage.py sqlmigrate blog 0001)
- The default sqlite3 database db.sqlite3 in the project root directory
View and edit db.sqlite3
- Using third-party software, SQLite Expert Personal lightweight, completely free of charge
- Run in the associated data path sqlite3 installed not open the database directly or else
- sqlite3.exe D:\pycharm\imooc\myblog\db.sqlite3
- Then double-click to open db.sqlite3
Step back ( page rendering data):
- Views.py中import models
- article = models.Article.objects.get(pk=1)
- render(request, page, {‘article’:article})
The front end of the step of:
- Templates can be used directly objects and object. "" Operation
- {{article.title}}
Amin Introduction: What is the Admin?
- Admin is a powerful Django comes with a feature automated data management interface
- Authorized users can manage databases directly on the Admin
- Django provides a number of customization features for Admin
Configure Admin
- python manage.py createsuperuser create a superuser
- Access Address: http://127.0.0.1:999/admin/
- Become Chinese modify settings.py in LANGUAGE_CODE = "zh_Hans"
Configuration Application
- Introducing itself in applications at admin.py models module (or inside the model classes)
- 编辑admin.py: admin.site.register(models.Article)
Use Admin modify the data:
- Click on the hyperlink to enter Article Article list page
- Click on any piece of data, enter the edit page modification
- A row of buttons below the edit page perform the appropriate action
Modify the default data display name:
- A method of adding under Article class
- python version selected according __str __ (self) (python3 this use) or __unicode_ (self)
- return self.tile
Blog page design
- Blog main page
- Blog article content page
- Blog writing page
Main page content:
- Title of the article lists, hyperlinks
- Published blog buttons (hyperlinks)
Write a list of ideas:
- Remove all objects in the database articles
- The article packaged into an object list, transmitted to the front
- The front page article with the title listed individually in the form of a hyperlink
Template For loop syntax:
- {% for xx in xxs %}
- HTML statement
- {% endfor %}
Blog article page:
- title
- Article Content
- Modify article buttons (hyperlinks)
URL arguments:
- After the parameters are written in the response function request, you may have default values
- URL regular expression r '^ article / (? P <article_id> [0-9] +) $
- URL regular group name and parameter name must be consistent
Hyperlink destination address:
- Href is behind the target address
- Template can be used "{% url 'app_name: url_name' param%}"
- And wherein app_name url_name are arranged in the url
url name of the function parameters
- Root urls, write include () the position of the second parameter, namespace = 'blog'
- Application of the URL in the write () the position of the third parameter, name = 'article'
- Depending on whether include references another url profile
Blog written page, the page content
- Edit the title bar
- Article content editing area
- Submit button
Edit the response function:
- Use of request.POST [ 'Parameter name'] acquired form data
- models.Article.objects.create (title, content) to create objects
- POST method relates to submit the form, the front page plus {% csrf_token%}
Two edit page ( thinking):
- There is a new article is empty, modify article
- There is an article article page modify objects
- Article ID
Blog writing interface to modify the data
- article.title = title
- article.save()
Temple filter, what is a filter?
- Write in the template, belong to Django template language
- You can modify variables in the template to display different content
How to use filters:
- {{value | filter}}
- Examples: {{list_nums | length}}
- Filters can be superimposed: {{value | filter1 | filter2 | ...}}
Django Shell
- It is a python interactive command-line program
- It automatically in our project environment
- We can use it to interact with our project
How to use DjangoShell?
- Python manage.py shell
- from blog.models import Article
- Article.objects.all()
What is the use?
- We can use the Django shell to do some debugging
- Unknown test methods
Admin: Creating admin configuration class
- Class ArticleAdmin(admin.ModelAdmin)
- 注册:admin.site.register(Article, ArticleAdmin)
Additional fields are displayed:
- List-display = (‘title’, ‘content’)
- List_display supports tuple and list
filter:
- List_filter = (‘pub-time’)
The relevant code Address: https://github.com/jiyanjiao/myblog