Django development process simple blog

What is Django?

  1. Django is a python-based advanced web development framework of
  2. It allows developers to be efficient and rapid development
  3. The highly integrated (do not have to create the wheel), free and open source

The current path creation project

         django-admin startproject myblog

   

 

 

 

  1. Directory name not propose to amend
  2. Wsgi.py python server gateway interface (an interface between the python with the web server application)
  3. Urls.py Django project, all addresses (pages) will need to configure their own URL
  4. Settings.py configuration database, etc.
  5. __init_ modularity, may be introduced

Create an application :

  1. Open a command line, enter the project in the same directory manage.py  
  2. Command line, type: python manage.py startapp blog
  3. Settings.py to add the application name in the Insalled_APPS

   

 

 

  1. migrations data migration (migration) module
  2. admin.py: the back-end application configuration management system (Django's own back-end management system)
  3. apps.py: some configurations currently applied automatically in the future Django-1.9
  4. models.py: a data module, using ORM framework structure similar to Models MVC (Model).
  5. 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

  1. Include the introduction of the root urls.py
  2. Urls.py file is created in the App Catalog, the same format as the root urls.py
  3. Url urls.py the root function of the second parameter to include ( 'blog.urls')
  4. Urls.py root URL for the configuration of App name, App is the total of all the URL's path
  5. When configuring url regular expression and the end symbol $ /

Templates Description:

  1. Templates file is stored HTMl
  2. Use the Django template language (Django Template Language, DTL)
  3. You can use a third-party templates (such as jinja2)

Where modifications:

  1. TEMPLATES Setting the value of the BACKEND

Development of a Template

  1. Create a directory called Templates in the root directory of the app
  2. Create html file in the directory
  3. Rendering to the browser render the views inside

return render(request, "index.html")

  1. Render () third argument to support a dict type parameter
  2. The dictionary is a backstage pass to the parameter template, the key is the parameter name
  3. Used in the template parameter name {} {} used directly

Django Find Template

  1. Finding Templates Django added in order INSTALLED_APP
  2. Templates directory of the same name .html file will cause conflict in different APP

TemplatesTemplates resolve conflict programs

  1. Create a directory called APP name in the Templates directory of APP
  2. Html files into the new directory under

Models Introduction

  1. Typically, a Model database corresponding to a data table
  2. In Django Models shown in the form of class
  3. Contains some basic fields as well as some of the acts of data

SNAKE

  1. Object-relational mapping (Object Relation Mapping)
  2. Achieve the mapping between objects and databases
  3. Hides the details of data access, does not need to write SQL statements

Write Models steps:

  1. Models.py created in the root directory of the application, and the introduction of models module
  2. Create a class that inherits models.Model, means one type of data tables
  3. Create fields in the class

Fields are created:

  1. I.e., properties of the class field (variable)
  2. 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:

  1. Manage.py command line to enter the directory statistics
  2. Execute python manage.py makemigrations app name (optional)
  3. In the execution python manage.py migrate

View:

  1. Django will automatically generate the migration file in app / migrations / directory
  2. Execute python manage.py sqlmigrate application file name id see the SQL statement (python manage.py sqlmigrate blog 0001)
  3. The default sqlite3 database db.sqlite3 in the project root directory

View and edit db.sqlite3

  1. Using third-party software, SQLite Expert Personal lightweight, completely free of charge
  2. Run in the associated data path sqlite3 installed not open the database directly or else
  3. sqlite3.exe D:\pycharm\imooc\myblog\db.sqlite3
  4. Then double-click to open db.sqlite3

 

Step back ( page rendering data):

  1. Views.py中import models
  2. article = models.Article.objects.get(pk=1)
  3. render(request, page, {‘article’:article})

The front end of the step of:

  1. Templates can be used directly objects and object. "" Operation
  2. {{article.title}}

Amin Introduction: What is the Admin?

  1. Admin is a powerful Django comes with a feature automated data management interface
  2. Authorized users can manage databases directly on the Admin
  3. Django provides a number of customization features for Admin

Configure Admin

  1. python manage.py createsuperuser create a superuser
  2. Access Address: http://127.0.0.1:999/admin/
  3. Become Chinese modify settings.py in LANGUAGE_CODE = "zh_Hans"

Configuration Application

  1. Introducing itself in applications at admin.py models module (or inside the model classes)
  2. 编辑admin.py: admin.site.register(models.Article)

Use Admin modify the data:

  1. Click on the hyperlink to enter Article Article list page
  2. Click on any piece of data, enter the edit page modification
  3. A row of buttons below the edit page perform the appropriate action

Modify the default data display name:

  1. A method of adding under Article class
  2. python version selected according __str __ (self) (python3 this use) or __unicode_ (self)
  3. return self.tile

Blog page design

  1. Blog main page
  2. Blog article content page
  3. Blog writing page

Main page content:

  1. Title of the article lists, hyperlinks
  2. Published blog buttons (hyperlinks)

Write a list of ideas:

  1. Remove all objects in the database articles
  2. The article packaged into an object list, transmitted to the front
  3. The front page article with the title listed individually in the form of a hyperlink

Template For loop syntax:

  1. {% for xx in xxs %}
  2. HTML statement
  3. {% endfor %}

Blog article page:

  1. title
  2. Article Content
  3. Modify article buttons (hyperlinks)

URL arguments:

  1. After the parameters are written in the response function request, you may have default values
  2. URL regular expression r '^ article / (? P <article_id> [0-9] +) $
  3. URL regular group name and parameter name must be consistent

Hyperlink destination address:

  1. Href is behind the target address
  2. Template can be used "{% url 'app_name: url_name' param%}"
  3. And wherein app_name url_name are arranged in the url

url name of the function parameters

  1. Root urls, write include () the position of the second parameter, namespace = 'blog'
  2. Application of the URL in the write () the position of the third parameter, name = 'article'
  3. Depending on whether include references another url profile

Blog written page, the page content

  1. Edit the title bar
  2. Article content editing area
  3. Submit button

Edit the response function:

  1. Use of request.POST [ 'Parameter name'] acquired form data
  2. models.Article.objects.create (title, content) to create objects
  3. POST method relates to submit the form, the front page plus {% csrf_token%}

Two edit page ( thinking):

  1. There is a new article is empty, modify article
  2. There is an article article page modify objects
  3. Article ID

Blog writing interface to modify the data

  1. article.title = title
  2. article.save()

Temple filter, what is a filter?

  1. Write in the template, belong to Django template language
  2. You can modify variables in the template to display different content

How to use filters:

  1. {{value | filter}}
  2. Examples: {{list_nums | length}}
  3. Filters can be superimposed: {{value | filter1 | filter2 | ...}}

Django Shell

  1. It is a python interactive command-line program
  2. It automatically in our project environment
  3. We can use it to interact with our project

How to use DjangoShell?

  1. Python manage.py shell
  2. from blog.models import Article
  3. Article.objects.all()

What is the use?

  1. We can use the Django shell to do some debugging
  2. Unknown test methods

Admin: Creating admin configuration class

  1. Class ArticleAdmin(admin.ModelAdmin)
  2. 注册:admin.site.register(Article, ArticleAdmin)

Additional fields are displayed:

  1. List-display = (‘title’, ‘content’)
  2. List_display supports tuple and list

filter:

  1. List_filter = (‘pub-time’)

The relevant code Address: https://github.com/jiyanjiao/myblog

 

Guess you like

Origin www.cnblogs.com/jiyanjiao-702521/p/11880379.html