The freed Jiang Ge--01 of the Django Starter sample (Installation and start-up)

Source: Internet
Author: User
Tags install django pip install django

The process of learning has always been slow. In order to improve efficiency, we should take the goal-driven method and learn from the side. Learning Python in the near future.

Django is a Web server framework under Python. There are many different frameworks under Python. Django is one of the most iconic players in the heavyweight class. Many successful websites and apps are based on Django. Although Django is not in Python, it is not rail to Ruby's unified, but Django is undoubtedly a leader in Python's Web application.

Django accidentally Quintin the same name as the movie. This greatly increases the visibility of Django. In addition, the use of the film, propaganda the pronunciation of this strange word.

Here is the first experience of Jiang GE.


1. Installing Django

Start Python on your computer and try to load the Django module. If it can be loaded successfully, then Django is already installed:

    1. Import Django
    2. Print (Django. VERSION)


If Django is not yet installed, you can try using PIP installation at the command line:

    1. sudo pip install Django

or use Easy_install:

    1. sudo easy_install Django


2. Start

Use the following command to create the project:

    1. django-admin.py Startproject MySite


In the current directory, the MySite folder is generated. Its file tree structure is as follows:

    1. MySite
    2. ├──manage.py
    3. └──mysite
    4. ├──__init__.py
    5. ├──settings.py
    6. ├──urls.py
    7. └──wsgi.py
    8. Directory, 5 files


Enter MySite and start the server:

    1. Python manage.py Runserver 8000

The 8000 above is the port number. If not stated, the port number defaults to 8000.

Open the browser, Access http://127.0.0.1:8000, you can see the server is already running:

Although there is a server that can run, but what content is not.

3. First Web page

As you can see in the HTTP protocol, the network server is the "request-response" mode of operation. The client sends a request to the URL, and the server starts the cook after the request, and finally dishes the guest. The MVC structure that Django uses, namely order, kitchen, storeroom separates.

We need a commander to assign the URL to an object processing, which requires a urls.py setting under Mysite/mysite. Python assigns URL requests to a chef based on the program.

MySite
├──manage.py
└──mysite
├──__init__.py
├──settings.py
├──urls.py
└──wsgi.py


Modify the urls.py to:

From
django.conf.urls Import patterns, include, url

From Django.contrib Import admin
Admin.autodiscover ()

Urlpatterns = Patterns ("',
# Examples:
# URL (R ' ^$ ', ' mysite.views.home ', name= ' home '),
# URL (R ' ^blog/', include (' Blog.urls ')),

URL (r ' ^admin/', include (Admin.site.urls)),
URL (r ' ^$ ', ' mysite.views.first_page '),
)


We added the last line. It assigns the URL of the root directory to an object for processing, and this object is mysite.views.first_page. The object used to process the HTTP request does not yet exist, and we create the views.py under Mysite/mysite and define the First_page function in it:

#-*-Coding:utf-8-*-

From django.http import HttpResponse

def first_page (Request):
Return HttpResponse ("<p> World Good </p>")


The first line describes the character encoding as Utf-8, which prepares for the use of Chinese below. The function of the First_page function is to return an HTTP reply, that is, <p> world good </p> here. First_page has a parameter request that contains the specific information requested, such as the type of the request, which is not used here.

The page works as follows:

4. Add App

A Web site may have multiple features. Instead of throwing everything into a folder, we can manage it in Django, app-based, modular. Under MySite, run manange.py to create a new app:

    1. $python manage.py Startapp West


This new app is called West and is used for Western food. In our root directory, there is a new folder called West.

  1. mysite/
  2. ├──manage.py
  3. ├──mysite
  4. │├──__init__.py
  5. │├──__init__.pyc
  6. │├──settings.py
  7. │├──settings.pyc
  8. │├──urls.py
  9. │├──views.py
  10. │└──wsgi.py
  11. └──west
  12. ├──admin.py
  13. ├──__init__.py
  14. ├──models.py
  15. ├──tests.py
  16. └──views.py


We also need to modify the project settings to indicate that we want to use West. In mysite/setting.py, in Installed_apps, add "West":

  1. Installed_apps = (
  2. ' Django.contrib.admin ',
  3. ' Django.contrib.auth ',
  4. ' Django.contrib.contenttypes ',
  5. ' Django.contrib.sessions ',
  6. ' Django.contrib.messages ',
  7. ' Django.contrib.staticfiles ',
  8. ' West ',
  9. )


You can see that in addition to the newly added West,django, some functional apps have been loaded by default, such as user authentication, session management, displaying static files, and so on. We will explain their use in the future.

5. Add App Page

We add the homepage for the app below. We previously set the URL Access object in mysite/urls.py. is still set in a similar way.

On the other hand, in order to decouple and implement modularity, we should set the URL Access object in west/urls.py. Specific as follows:

First, modify the mysite/urls.py:


From Django.conf.urls import patterns, include, url

From Django.contrib Import admin
Admin.autodiscover ()

Urlpatterns = Patterns ("',
# Examples:
# URL (R ' ^$ ', ' mysite.views.home ', name= ' home '),
# URL (R ' ^blog/', include (' Blog.urls ')),

URL (r ' ^admin/', include (Admin.site.urls)),
URL (r ' ^$ ', ' mysite.views.first_page '),
URL (r ' ^west/', include (' West.urls ')),
)



Note the newly added last line. Here, we remind the Commander, for West/'s visit, to refer to west/urls.py. Then we create the west/urls.py and add the content:

From Django.conf.urls import patterns, include, url

Urlpatterns = Patterns ("',
URL (r ' ^$ ', ' west.views.first_page '),
)



The URL corresponds to the First_page function in west, views.py. Finally, under West, modify the views.py to:

    1. #-*-Coding:utf-8-*-
    2. From django.http import HttpResponse
    3. def first_page (Request):
    4. Return HttpResponse ("<p> Western food </p>")


Visit Http://127.0.0.1:8000/west to see the effect.

6. Summary

As you can see, the Django creation process is very simple. But this is just the first attempt at Django. To create a full-featured website, you also need to invoke many of the other Django features.

The freed Jiang Ge--01 of the Django Starter sample (Installation and start-up)

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.