How to build a website easily and easily-based on Django

Source: Internet
Author: User
Tags install django sublime text

I. Required tools and related environment

1. System: WIN7,WIN8.1,WIN10 (pro-Test available-this is win7,64 bit)

2. The version used in this article is:

1) python-2.7.11 "Baidu Cloud Disk Share:http://pan.baidu.com/s/1c25M2ty"

2) Django-1.6.11 "Baidu Cloud Disk Share:http://pan.baidu.com/s/1nvbbXWH"

Second, build the development environment (note: The following installation process involved in the path, according to the user's own custom)

1. Install python: Download the installation package (for example: Python-2.7.11.msi), then you only need to use the default settings, always click "Next" until the installation is complete.

--Configure Environment variables: C:\My_softwares\Python27 (depending on the path you installed), 1.1.

---Check if the installation was successful: "Ctrl + R" and input "PowerShell (or cmd)" and input "python"--as shown in result 1.2.

Figure 1.1 Configuring the environment variable diagram 1.2 Python installation and configuration success

2. Install Django:

1) Download the Django installation package, e.g. (Django-1.6.11)

--Unzip and place the Python installation directory in the same root directory (also available in the Python directory, e.g., C:\My_softwares\Python27---C:\My_softwares\Python27\Django-1.6.11)

--Go to the Django directory via "PowerShell" (or cmd), execute "python setup.py install" and start the installation, Django will be installed into Python's lib under Site-packages.

2) Configure environment variables (operations are similar to configuring Python's environment Variables):

--C:\My_softwares\Python27\Lib\site-packages\django

--C:\My_softwares\Python27\Scripts ( Note: After the Django installation is complete, "django-admin.py" is generated under this target for subsequent site establishment )

--once added, you can create a new project using the commands provided by "django-admin.py".

3) Check if Django is complete:

--"CTRL + R"--input "powershell (or cmd)"--Enter "Python"

--Enter "import Django"--Input "Django." version "and whether the corresponding versions are displayed (such as: 1, 6, one, ' final ', 0), 1-3 are shown.

Figure 1-3 Django Installation and configuration success

3. PoWershell Introduction: Windows PowerShell is a command-line shell and scripting environment that enables command-line users (such as Linux-like "ls" commands to display files contained in the current directory) and script writers to take advantage of.

1) Getting Started with Windows PowerShell is primarily for IT pros, programmers, and advanced users who don't have background knowledge of Windows PowerShell.

2) How to use: "Ctrl + R"--enter "PowerShell".

3) Use "PowerShell" for quick editing, 1-4:

Figure 1-4 Opening the quick edit mode for PowerShell

Third, create the first website (create a project under the conditions of environment construction, this article uses "POWERSHLL" to make command line interaction)

1. Copy the file "django-admin.py" from "C:\My_softwares\Python27\Scripts" to directory A (directory a indicates that you intend to create a Web site project under directory A, where directory A is: D:\16_ Grade_xmu\code_xmu\turingclass_online)

2. Enter directory a Via "Powershll", entering the statement for the Django-created project, as follows:(refer to: http://www.runoob.com/django/django-first-app.html)

1) "Ctrl + R"--"PowerShell" Open command Interactive window--input "python. \django-admin.py startproject turingclass" Turingclass is the project name and depends on the user.

2) After creation we can look at the directory structure of the following project, 2-1 shows:

Figure 2-1 Initial creation of the project structure

3) The documents are described as follows:

    • "manage.py" a useful command-line tool that allows you to interact with the Django project in a variety of ways.
    • "__init__.py" an empty file that tells Python that the directory is a python package.
    • "settings.py" the setup/configuration of the Django project.
    • "urls.py" the URL Declaration of the Django project; A "directory" of Django-driven Web sites.
    • "wsgi.py" a WSGI-compatible WEB server's portal to run your project.

3. At this point, we have been able to start the local server through the ". \manage.py" of the above production, the specific command is as follows:

1) Enter the command "python. \manage.py runserver 127.0.0.1:8000", after success as shown in 2-2.

2) where 127.0.0.1 is the port number for the local server ip,8000. If the port number is not specified, the default is Port 8000, and the command statement can be "python. \manage.py runserver", which defaults to "127.0.0.1:8000"

3) Open the browser, enter "http://127.0.0.1:8000/", you can open the Web page, 2-3 shows.

Figure 2-2 running a local IP success

Figure 2-3 (Note: If the code changes in the project, the server will automatically monitor the code changes and reload automatically, so if you have already started the server you do not need to restart manually)

4. Create the app and configure the appropriate files (the app is divided according to the user's function module):

1) under Directory A (here is D:\16_Grade_XMU\Code_XMU\TuringClass_OnLine), enter the command "python . \manage.py Startapp App_ Turingclass"Create an app where App_turingclass is the name of the app and depends on the user's habits.

--Note1: Each time you create an app, you need to add app_name in the "setting.py" file in the project directory Installed_apps (This is "app_turingclass 】)!

2) in the App_turingclass directory, manually create a "urls.py", and then the project directory (here is D:\16_Grade_XMU\Code_XMU\TuringClass_OnLine\ Turingclass) under urls.py contains all the urls.py in the app, as shown in principle 2-4 (as I understand it).

Figure 2-4 urls.py

3) Create the Templates folder, under normal circumstances, the user put all the HTML files in this directory, the specific operation is as follows:

A) Create the folder "Templates" under the project root directory

B) The "setting.py" file configuration file in the project directory, as shown in 2-5:

Figure 2-5: Configuration templates

4) Create the HTML under the Templates folder, such as "home_turingclass.html", and write the code. Here you need to understand the following view (view.py) and URL configuration , as follows:

A) Each app_name directory has a "view.py", responsible for the implementation of the background functions of the site, data management and interaction, through the ulrs.py call views.py corresponding function, and then returned by the function of the specified HTML (i.e. interface).

--such as:" url (r ' ^$ ', ' App_TuringClass.views.Home_TuringClass ') ", call Home under App_turingclass directory The _turingclass function.

-such as: "Return render (request, ' home_turingclass.html ', context) ", Return to home_turingclass.html and display to the user.

B) The understanding of the urls.py configuration is also very important, 2-6 for Django to create a Web site operating mechanism (self-collation, error, I will understand and improve), specifically see "HTTP/ django-chinese-docs-16.readthedocs.io/en/latest/"

Figure 2-6 Django operating mechanism

5) How to use the background (views.py) variables in the HTML file: effect 2-7 shows

A) views.py:

--Assigning the dictionary context variable: context['string' = "Hello Turing Class from view.py!"

--Return to the context variable: return render (Request, ' home_turingclass.html ', context)

B) as shown in Html,2-8:

--Use the string variable returned in the background: {{string}} to display "Hello Turing Class from view.py!"

Figure 2-7 the interface diagram shown to the user 2-8 home_turingclass.html

5. Create the static folder (the same level as the Templates directory), the project involves the CSS, JS and other files are placed in the static directory for management:

1) configuration file: In the project directory under the "setting.py" file configuration, 2-9 is shown:

Figure 2-9 Configuring the static

6. The next step is to develop the entire site, based on the requirements of the project, module partitioning.

------------------------------------------------------------Summary------------------------------------------------- -----------

1. Install Python and Django and configure the environment variables;

2. Copy the django-admin.py file to directory A (a means you want to create the project in this directory);

3. Enter "python django-admin.py startproject project_name" to create the project;

4. Enter "python. \manage.py runserver 127.0.0.1:8000" Run the local IP, run the project, you can select the browser, enter the URL "http://127.0.0.1:8000/" to open the Web page;

5. Enter "python. \manage.py startapp appName" to create the app project and configure it as follows:

1) manually add urls.py;

2) Configure "setting.py", including appname, templates, static and so on (subsequent database types are also configured here);

3) Understand the relationship of urls.py under each appname under the urls.py and ProjectName directory, than the configuration;

4) write "views.py" in the appname directory, return variables and specific pages;

6. Enter "python. \manage.py sycndb" To create a database, this article does not mention this part of the content, the next time to strive for a deeper understanding, and then organize.

--------------------------------------------------------------------------------------------------------------- ----------------

Iv. recommendation of related websites

1. Django, Python (rookie tutorial): http://www.runoob.com/

--Django Chinese document:http://django-chinese-docs-16.readthedocs.io/en/latest/

2. Sublime:

1) official website (download):http://www.sublimetext.com/

2) package Control:http://jingyan.baidu.com/article/925f8cb817fd49c0dce05653.html

3) ement: http://www.cnblogs.com/tinyphp/p/3217457.html

4) Full stack development necessary 10 Sublime text plugin:http://www.php100.com/html/it/focus/2014/1128/7935.html ;

--Note1: In the case of installing the "package Control", to download the plug-in, first open the console (ctrl+shift+p) input the install package, and then enter the plug-in name.

3. Git

1) Code cloud:https://git.oschina.net/

2) GIT-install package:Https://git-scm.com/download/win ;

3) Tortoisegit-installation package:https://tortoisegit.org/download/

--------------------------------------------------------------------------------------------------------------- ---------------

This article according to my development experience to summarize and collation, if found inappropriate places, but also to correct, learn from each other!

How to build a Web site easily and easily-based on Django

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.