1024programmer Java First impressions of Django

First impressions of Django

Django (/ˈdʒæŋgoː/) is an open source web development framework (open source web framework) written in python language, which encourages rapid development and follows the MTV design pattern. Its main purpose is to develop database-driven websites easily and quickly.

Django abides by BSD copyright. The latest stable version (stable) is v1.3.1 released in September 2011.

Django’s name comes from the Belgian jazz musician Django Reinhardt. He is a gypsy who mainly plays guitar.

Like rails, Django emphasizes Loose coupling, Write less code, Rapid development, DRY, Consistency and other principles. But the biggest difference in design philosophy between Django and rails is that Django emphasizes “Showing instructions over implicit meanings“, which comes from the core principles of Python.

This difference in design philosophy makes Django very different from rails: Django doesn’t like “magic code”. Although magic code looks cool, it has the following problems:

1) The syntax is obscure and difficult to understand. In addition to the programming language, you also need to know a custom DSL
2) Compared with programming languages, DSL is more unstable and may change frequently
3) If You need to spend a lot of effort to understand its implementation
4) Magic code can easily implement certain specific functions, but if you want to make some changes on this basis, it will become very difficult
br> Therefore, Django does not like “magic code”, but emphasizes that the code is understandable and controllable.

For example, the example used to attract people at the beginning of “Agile Web Development with Rails” is a Model class about the project:

class Project < ActiveRecord::Base
          belongs_to :portfolio
          has_one :project_manager
          has_many :milestones
          has_and_belongs_to_many :categories
         
          validates_presence_of :name, :description
          validates_acceptance_of :non_disclosure_agreement
          validates_uniqueness_of :key
 end

It looks amazing, right? But you’ll be asked a few questions right away:

1) In addition to the association relationship, where are the attributes of the object itself defined?

2) What is the attribute of non_discolosure_agreement?

3) How many types of syntax are there like validates_xxx?

This article is not mainly about introducing rails, so the above questions will not be answered here. But in order to compare with Django’s Model, another part of the rails model needs to be completed: migration. In fact, in rails, the above Model also needs a db migrate class to work:

class CreateProject

 


Django is designed to define the data model in the form of Python classes and connect it to the database through ORM. At the same time, it is agreed that in order to avoid "guessing games", one should not rely solely on the naming of fields to indicate their possible behavior, but rather define operations through key parameters.

The above Model class is implemented in Django in the following simple and clear way:

class Project(models.Model):

     portfolio = models.ForeignKey(Portfolio)

     categories = models.ManyToManyField(Category)
     name = models.CharField()
     description = models.TextField()

      #For relationships such as has_one and has_many, you only need to declare the ForeighKey on the opposite end. You do not need to declare it on the local end and can directly reference it

Django's code is clear and every statement is clear. That's the Django style. (By the way, Django's flexibility does not come at the expense of foreign key relationships, which are not supported in rails' database)

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/764814

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索