Web development of Django Model learning--python

Source: Internet
Author: User
Tags codeigniter

Django is a relatively old and powerful framework, although it has a lot of shortcomings, but still decided to start with this framework to learn. This series is a simple learning note for future reference.

The background of the site was originally written in PHP codeigniter framework, the database only 4 tables, involving the most basic product additions and deletions and transduction and other operations. Now when you swap with Django, all the background including templates have to be rewritten. Although it looks like a lot of work, it takes three days to learn Django and then spend three days rewriting all the background rewrite (excluding the tedious process of deploying to an egg-sore SAE), which makes Django's development efficiency so high.

1. Introduction of introduction 1.1

Django is a Web application framework written in Python that is open source and uses the MVC design pattern. But in Django, the part of the controller that accepts user input is handled by the framework itself, so Django is more concerned with models, templates, and views, called MTV mode. Their respective responsibilities are as follows:

    • Model, the data Access layer: All transactions related to the data, i.e. how to access it, how to validate it, what behavior it contains, and the relationship between the data.
    • Template, Presentation layer: Handles performance-related decisions, that is, how to display in a page or other type of document.
    • View, business Logic layer: Access the model and adjust the relevant logic of the appropriate template. The bridge between the model and the template.

Django's MVC programming approach is different from CodeIgniter, where the business logic is placed in the controller, and the Django MVC controller is partially implemented by urlconf. The urlconf mechanism is to use regular expressions to match URLs and then invoke the appropriate Python function. It's a step deeper than what the MVC framework considers, because most of our programmers write programs in the control layer. Now this work is given to the framework, which requires only a small number of calling code to be written, which greatly improves productivity.

1.2 Features

The core of the Django framework includes an object-oriented mapper as a data model (defined in the form of Python classes) and a medium between relational databases, a URL dispatcher based on regular expressions, a view system for processing requests, and a templating system. The core framework also includes:

    • A lightweight, stand-alone Web server for development and testing.
    • A form serialization and validation system for converting between HTML forms and data that is suitable for database storage.
    • A caching framework, and several caching options are available.
    • Middleware support that allows interference in all stages of request processing.
    • The built-in distribution system allows components in the application to communicate with each other using predefined signals.
    • A serialization system that can generate or read a Django model instance in XML or JSON notation.
    • A system for extending the capabilities of the template engine.

(referenced above from Wikipedia)

2 preparatory work 2.1. Installation

When I studied Django, I looked at the Django book, which is a great place to get started and highly recommended. Description: The following are all based on Windows7,python 2.7.6 and Django 1.6.1, when installing the system and the framework version. Django should be installed in the same way as the official website, the database is MySQL for example, and other if discrepancies refer to the above Django book bar.

After the installation is complete, open the Python interpreter, enter the following statement, if the display version number is installed successfully.

>>> import django>>> django.VERSION
2.2 Creating a Django Project

After the Django installation succeeds, it will bring a django-admin.py administrative script (which is added to the system variable path by default), and if the console cannot be run directly, you can python安装目录\Scripts find the file in. Run the command django-admin.py startproject mysite . This will create a directory MySite in your current directory.

The directory created by the Startproject command contains 4 files:init. PY (let Python treat the directory as a development package, which is the file required for a set of modules. This is an empty file, typically you don't need to modify it), manage.py (a command-line tool that allows you to interact with the Django project in a variety of ways), settings.py (the Django project's settings or configuration), urls.py (the URL setting for the Django project, which is visible as the directory for your Django site),

Once created, start a local server in the MySite directory and then access it in the python manage.py runserver browser to http://127.0.0.1:8000/ see the initial page of the newly created project.

The next chapter will explain Django's most basic URL configuration.

3 URL Configuration

First we create the views.py file in the project folder for later use.

# views.py    from django.http import HttpResponsedef hello_view(request):    return HttpResponse("Hello world")

We created the simplest and most basic view, with only a function called Hello, and returned a HttpResponse object. Although we do not actually write this in the project, it is enough to support the basic Django operation: A "Hello World" is displayed on the page. The next URL configuration is the focus.

The

URL configuration is like the directory of the Web site that Django supports. Its essence is the URL pattern and the mapping table between the view functions to invoke for that URL pattern. That's how you tell Django to call that code for that URL and call that code for that URL. For example, when a user accesses/foo/, the View function Foo_view () is called, and this view function exists in the Python module file view.py. Edit the urls.py and enter the following content.

  from   Django   conf   urls   defaults  import    *    from   MySite   views  import   Hello_viewurlpatterns  =   patterns   (  "     (  ' ^hello/$ '    Hello_view     )   

http://127.0.0.1:8000/helloyou can see your first page as soon as you access it in the browser. The key to this is to match the URL with a regular expression, which contains a caret (^) and a dollar sign () that match the head and tail of the string, respectively. For example "^hello/", any URLs that start with/hello/, such as "/hello/foo", will match. Another example, "^" represents an empty string, matching the root URL.

Most dynamic Web applications, URLs usually contain relevant parameters, such as when you click a post to jump to the detailed page of the post (/list/123), where you can use d+ to match more than 1 numbers, with \d{1,2} to match one or two digits, the corresponding configuration is as follows:

urlpatterns = patterns(‘‘,    (‘^$‘, index_view),    (‘^hello/$‘, hello_view),    (‘^list/\d{1,2}/$‘, list_view))

When this URL is set, the corresponding view function should add a parameter to receive, as shown in the example view function def list_view(request, id): requestId = int(id) .

Django Model Learning--python Web development

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.