2. First Django Project
In the previous section, we completed the installation of Python,django and databases, and now we are going to create the first Django project and take the first step toward using Django to develop the application.
2.1. Create a first Django project
We create a working directory where we store Django, example:C:\My files\python Projects in the Command Prompt window into the directory you just created, run the Run command:
django-admin.py Startproject MySite
This will create a directory MySite in your current directory .
Note: Make sure that the Windows of the PATH The environment variables are increased by django-admin.py file path: C:\Python27\Scripts If you do not add a path, you can run the following command :
Path%path%; C:\Python27\Scripts
The Startproject command creates a directory with the following contents:
mysite/manage.py mysite/ __init__. py wsgi.py settings.py urls.py
The catalogue and documentation are described below:
- manage.py : A command-line tool that allows you to interact with the Django project in a number of ways. Type python manage.py Help to see the assistance.
Sub- mysite/ file:
- __init__.py : Let Python treat the directory as a required file for a development package (that is, a set of modules). This is an empty file and it is generally not necessary to modify it.
- settings.py : This file is a setup or configuration file for a Django project.
- urls.py: The URL profile for the Django project, which is the directory for your Django website.
- wsgi.py: Wsgi configuration of the Django project, Web server Gateway Interface file (Python Web server, Gateway Interface, abbreviated to WSGI).
2.2. Run the development server
The Django Development Service is available during development, a built-in, lightweight Web service. 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 automatically loads it, and changes the code without restarting the service to see the results.
Switch to the project directory (CD mysite ) and run the following command:
Python manage.py runserver
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/. We can see a light blue Django Welcome page.
It began to work, such as:
2.3.Hello World Page
Our first goal is to create a Web page to output this famous example message: Hello world.
If you have ever published the Hello World page, but have not used the web frame, simply enter Hello World in the hello.html text file and upload it to a Web server, such as http://127.0.0.1/ Helloworld.html
Note that there are two key messages: ① A page that includes the string "Hello World"; ② page URL (http://127.0.0.1/helloWorld.html).
Using Django to do a Web page would be different, and we would do this two key messages differently. The content of the ① page is implemented by View function, ②url defined in the URLCONF configuration information of the urls.py file.
First, let's write a function about the Hello world view.
First view:
In the previous chapter we used the MySite folder created by django-admin.py Startproject, we manually created an empty file called views.py. This file will contain the view definition information for this chapter. The view.py file is named Django, making it easy to understand and read code.
Our Hello world view is very simple. is a complete function and import declaration, you need to enter the contents of the views.py file as follows:
from Import def HelloWorld (Request): Return HttpResponse ("Hello World ")
Description: A Django view is a function of Python. The type of the first parameter of this function is HttpRequest; it returns a HttpResponse instance. Below we will make our first Python function a Django-recognizable view.
2.3.1. Your first urlconf
We understand the urlconf configuration information as a directory of Django websites. It is essentially a mapping table that defines the URL pattern and the view functions to invoke for that URL pattern. With this mapping table, Django knows that the specific view function code needs to be executed for a particular URL access. For example, when a user accesses/HelloWorld/calls the View function HelloWorld (), this view function exists in the Python module file view.py.
When you execute django-admin.py startproject in the previous chapter, you automatically create a copy of the URLconf (that is, the urls.py file). The default urls.py will look like this:
fromDjango.conf.urlsImportpatterns, include, url#uncomment the next lines to enable the admin:#From django.contrib Import admin#Admin.autodiscover ()Urlpatterns= Patterns ("', #Examples: #URL (r ' ^$ ', ' mysite.views.home ', name= ' home '), #URL (r ' ^mysite/', include (' Mysite.foo.urls ')), #Uncomment the Admin/doc line below to enable admin documentation: #URL (r ' ^admin/doc/', include (' Django.contrib.admindocs.urls ')), #uncomment the next line to enable the admin: #URL (r ' ^admin/', include (Admin.site.urls)),)
We want to add a URL and view to urlconf, just add the python tuple that maps the URL pattern and view functionality. The following shows how to add the HelloWorld feature in view:
fromDjango.conf.urlsImportpatterns, include, url fromMysite.viewsImportHelloWorld#uncomment the next lines to enable the admin:#From django.contrib Import admin#Admin.autodiscover ()Urlpatterns= Patterns ("', #Examples: #URL (r ' ^$ ', ' mysite.views.home ', name= ' home '), #URL (r ' ^mysite/', include (' Mysite.foo.urls ')), #Uncomment the Admin/doc line below to enable admin documentation: #URL (r ' ^admin/doc/', include (' Django.contrib.admindocs.urls ')), #uncomment the next line to enable the admin: #URL (r ' ^admin/', include (Admin.site.urls)), ('^helloworld/$', HelloWorld),)
Save the file, and if you do not start the Django Development server to test the modified URLconf, run command line python manage.py runserver.
If it has been running for a while, it does not need to be restarted manually, and the development server will automatically monitor code churn and reload new information automatically. Just refresh the page http://127.0.0.1:8000/you can see:
For example, the Web site already sees our mapped page directory.
Open your browser to access http://127.0.0.1:8000/helloworld/. You can see the output. The development server will automatically detect changes in Python code to do the necessary reloading. Open the browser and enter "http://127.0.0.1:8000/helloworld/" directly, we will see the first Django view output we created in Hello World.
Now we have successfully created the first Django Web page.
2.4. Summary
The Django site and the original site page will be a big difference, the traditional site of the static page file, in the Django framework has become a corresponding view function, need our attention. The next section describes how Django implements Web pages by dynamically stitching HTML page code. This article is mainly for beginners to provide Django introduction and some practical examples of content. For more information on how Django runs and loads, refer to the Django Book website.
Getting Started with Python development and combat 2-the first Django project