[Python] using Django for Web Development series (i)

Source: Internet
Author: User
Tags django website install django

1 write in front

  In the absence of contact with the Internet industry, I have been very curious about how the site was built. Now, although engaged in internet-related work, but also has not been exposed to web development and other things, but the interest is still to have, and it is necessary to practice their own hands. There are many ways to develop web, such as traditional. Net, and very hot java. As a flexible and studious scripting language, Python has become more and more popular with programmers, even as a necessary skill for programmers. Django is a Python web-open framework, and many people say that learning Python is about Django, and it can also be seen that Django is powerful. Bo Master is also just contact with Django, purely personal interests, hope and the vast number of Bo friends to study and discuss together.

2 What is Web development?

Before we introduce what web development is, let's briefly mention the familiar www. www (World Wide Web) for short. Generally speaking, www is a set of technical specifications, it contains a lot of technology and protocols, such as HTML technology, URLs, HTTP protocol and so on. Through this set of technical specifications, we can describe the resources of the host on the Internet network, and then access these resources through a WWW client. The common WWW client has ie, Foxfire and other browsers.

With the advent of WWW technology, people can access resources on any remote computer on the Internet, or communicate with remote computers, in any corner of the world through a browser. Because of the advent of WWW technology in the 20th century, the concept of "global village" was born.

The web is a resource used to represent an Internet host for external access. In English, the web means the Web page, and the Web page is also referred to as the Web resource. Web resources on the Internet for external access are grouped into the following two categories:

  Static Web resources: refers to the Web page for people to browse the data is always the same, for example, HTML pages;

  Dynamic Web resources: refers to the Web page for people to browse the data is generated by the program, at different points of time to access the Web page to see the content varies.

  Microsoft's definition of Web development: Web Development is a broad term used to refer to Web pages or Web site authoring processes. These pages may be simple text and graphics that resemble documents. The page can also be interactive, or display a change of information. Writing interactive Server Pages can be a little more complicated, but it's possible to implement richer Web sites. Most of today's pages are interactive and offer modern online services such as shopping carts, dynamic visualizations and even complex social networks.

   the popular saying, the WebDevelopment is what we say to do the website. it is divided into Web Parts and the logical part is what we call foreground and background, < Span lang= "ZH-CN" > the foreground is responsible for interacting with the user, displays the data. with html display data, css control style, js writing complex interactions. background writing procedures for handling these logic, can be used c#,java,php

Now Web apps are tied to our lives, to our blogs, to our space, to big social networking sites like Facebook, everyone. more complex such as e-commerce in the C2c,b2b and other sites, have brought us a lot of convenience.

So What is the difference between WEB development and CS development ?

c/S structure, that is, client/server (client/server) structure, is well-known software system architecture, through the rational allocation of tasks to the client side and server side, reduce the communication overhead of the system, can make full use of the advantages of both ends of the hardware environment.

b/s structure, that is, Browser/server (browser/server) structure, with the rise of internet technology, a change of C/s structure or improved structure. In this structure, the user interface is implemented entirely through the WWW browser, some transaction logic is implemented at the front end, but the main transaction logic is implemented on the server side, forming the so-called 3-tier( three layer architecture ) structure. The b/s structure uses the browser technology which is mature and popularized to realize the powerful functions that need complex special software to realize, and saves the development cost, is a new software system construction technology, which becomes the preferred architecture of today's application software.

  First of all, it must be emphasized that the C/s and B + and no essential difference: b/S is based on a specific communication protocol (HTTP) C/S architecture, that is, b/S is included in C/s, is a special C/s architecture

Fundamentally , CS development is more suitable for developing single-machine applications , or for comparing fixed business logic with high-hardware-demanding programs . For example : Some application software, such as anti-virus software , teaching software these with the server logic less interaction , and the program and the client hardware has close contact with the program .

and BS development tends to e-commerce, social networks and other services need to closely cooperate with the software . because the client has only one browser , the development is convenient, the user experience is concise , but is limited by network communication , must consider the server performance.

3 What is Django?

Borrow a passage from Djangobook to explain what Django is. "Django is a Web development framework that enables web development to work happily and efficiently. With Django, you can build and maintain high-quality web applications at the lowest possible cost. "

  On the bright side, Web development is exciting and creative, and on the flip side, it's a tedious and annoying job. By reducing repetitive code, Django allows you to focus on interesting and critical things in Web applications. To achieve this goal, Django provides a high level of abstraction of the common Web development model, provides a quick solution to frequent programming jobs, and provides a clear and unambiguous convention for "How to solve problems". At the same time, Django tries to leave some way for the user to develop outside the framework as needed.

4 MVC Framework Introduction

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

Simply put, MVC is a method of software development that separates the definition of code and the method of data access (the model) from the request logic (Controller) and the user interface (view). The key advantage of this design pattern is that the various components are loosely coupled. Thus, each Django-driven Web application has a clear purpose and can be changed independently without affecting other parts. For example, a developer changes a URL in an application without affecting the underlying implementation of the program. Designers can change the style of the HTML page without touching the Python code. The database administrator can rename the data table and simply change one place without needing to find and replace it from a large stack of files.

5 Python Django Installation

First, download the Django version for your Python on the Django website, and now the latest version of the official release is Django1.8.2. Before you install Django, make sure that you have successfully installed Python.

  Installing Django under Windows system:

Unzip the downloaded Django package, locate the setup.py file, and then open the CMD Command window and execute the following command in its corresponding path:

Python setup.py Install

  Installing Django under a Linux system:

Enter the following command in the shell: 

$ tar xzvf django-*. tar.gz$ cd Django-*$ sudo python setup.py install

  Confirm that the Django installation is successful:

Under the Python IDE, enter:

Import Djangodjango. VERSION

If you see the following results, your Django has been successfully installed:

' Final ', 0)
6 Running the development server

Once you have installed the Python,django and (optional) database and associated libraries, you can take the first step toward developing a Django application by creating a project.

First, start creating a new project first.

Locate your django-admin.py file and add it to the system path. If the setup.py tool is installed, the django,django-admin.py should have been added to the system path. The landlord's django-admin.py directory is C:\Python27\Lib\site-packages\Django-1.8.2-py2.7.egg\django\bin.

Enter this directory and run the following command to create a new project:

Python django-admin.py startproject MySite

  The startproject command creates a directory that contains a folder named MySite and a file named manage.py. Where the MySite folder contains four files, respectively:

__init__.py

settings.py

urls.py

wsgi.py 

For more experience after installation, let's run the Django Development server to see our quasi-system. The Django Development Service is available during development, a built-in, lightweight Web service. We provide this server to allow you to develop your site quickly, meaning that there is no need to configure a product-level WEB server (such as Apache) before you are ready to release your product. The development server monitors your code and loads it automatically, so you can easily modify the code without restarting the service. If you have not started the server, please switch to your project directory (CD MySite) and run the following command:

Python manage.py runserver

You'll see something like this:

Django version 1.8.2, using Settings ' mysite.settings '
Starting development Server at Http://127.0.0.1:8000/
Quit the server with Ctrl-break

This will start a local server on port 8000 and can only be connected and accessed from your computer. Now that the server is up and running, use a Web browser to access Http://127.0.0.1:8000/. You should be able to see a pleasing light blue Django Welcome page. It's starting to work.

The interface looks like this:

Note: Although the Web server that Django comes with is convenient for development, never use it in a formal application deployment environment. At the same time, the server can only reliably process a single request and does not perform any type of security audit.

Change the host address or port of the development Server. By default, the Runserver command starts the development server on port 8000 and listens only for local connections. To change the server port, you can pass the port as a command line parameter:

Python manage.py runserver 8080

By specifying an IP address, you can tell the server – Allow non-local connection access. This feature is especially useful if you want to share the same development site with other developers. "0.0.0.0" This IP address, tells the server to listen to any network interface.

Python manage.py runserver 0.0.0.0:8000

When these settings are complete, the other computers on your local network will be able to access your IP address in your browser. For example: Http://192.168.1.103:8000/. (Note that you will need to inspections your network configuration to determine your IP address on the local network) UNIX users can enter ifconfig at the command prompt to obtain the above information. For users who use Windows, try the ipconfig command.

Well, the series of web Development using Django (a) is complete, I hope you follow my subsequent study notes, in order to study and discuss together.

7 Reference Content

[1] The Django book

[Python] using Django for Web Development series (i)

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.