Frist Django app-First, create a project

Source: Internet
Author: User

Origin

Since Python has learned, it is brief encounter to learn Python, a new programming language gives me a new way of thinking, to consolidate what I've learned and to learn more about Python, I want to learn about Django and see if it will bring me new things about the web. To record their own learning process here, and found that every time you write a blog is to learn the things of the finishing and promotion.

Create a new Projectstartproject

Python and Django installations are no longer detailed, and my environment is python2.7,django1.9.7.

Django-admin Startproject MySite

The above sentence creates a project name bit MySite, the name can be modified, and the directory structure is as follows:

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

For an introduction to several of the above files:

manage.py: command-line tools that interact with Django, such as building database table structures later based on model, servers for development use, etc. using the tool, using Python in manage.py's sibling directory manage.py You can see a list of commands that you can use.

MySite: This is the package name of the project.

__init__.py: Indicates that MySite is a package.

Setting.py:Django configuration files, including engineering app configuration, database configuration, language configuration, etc.

Urls.py:Django dispatcher, map to different views depending on the URL.

Wsgi.py:WSGI is the Web server gateway interface, which is the entry point for project to conform to this Protocol (ENTRY-POINT)

Runserver
Python manage.py runserver

Run this command under Manage.py's sibling directory to see the following:

not ' python manage.py migrate ', 2016-09:20:57 '  mysite.settings'starting Development Server at http://127.0.0.1:8000/Quit The server with CONTROL-C.

We successfully created a project, entered http://127.0.0.1:8000/in the browser , and saw that the following screen shows success:

Django comes with a development server that is easy to use (Django officially insists it cannot be used as a production environment), the default port number is 8000, and if you want to run on a different port number, run it in the following way, for example: 8080

Python manage.py runserver 8080
# If you want to access your site from other computers on your LAN
Python manage.py runserver 0.0.0.0:8080

Press CTRL C to stop the server.

Create a new app

One project can have multiple apps, and an app can belong to more than one project at the same time. Create an app with the following command (in the project directory)

Django-admin Startapp Polls

Create the directory structure after the app, because Django will automatically create a series of files, and we need to understand the role of each file

__init__ . Py│├──settings.py│├──urls.py│└──wsgi.py└──polls    ├──admin.py    ├──apps.py    __init__
    . py    ├──migrations    __init__. py    ├──models.py    ├──tests.py    └── views.py

root directory of the Polls:app

Admin.py:Django has a management interface that can register the model and manage it in the interface.

__init__.py: Indicates that polls is also a package

Migrations: Used to initialize the database, when executing python manage.py makemigrations will automatically generate a file here

__init__.py: Indicates that migrations is also a package

models.py: Define model class in this file

tests.py: Write test code

views.py: View, Django map urls.py inside the URL, in the views.py to find the corresponding processing method

Add view

Edit views.py File

 from Import Render  from Import HttpResponse # Create your views here. def Index (Request):     return HttpResponse ("helloWorld")
Add Mappings

Create a new urls.py file under the polls directory

 from Import URL  from Import  = {    URL (r'^$'index'), }

Tell Django of polls urls.py to add to mysite/urls.py

 from  django.conf.urls import   URL, include  from  django.contrib import   adminurlpatterns  = [URL (r   '  

Put us in the polls package below the urls.py include. It is noted that polls/urls.py is equivalent to a mysite/urls.py below a sub-file, the two are similar, the first parameter of the URL is a regular expression, the second parameter is the map to the view or the containing class, notice that there is a URL (r '^admin/', admin.site.urls), This is the configuration of the Django Admin interface, and the regular expression we write is the URL that starts with polls/, which means that all URLs that start with that prefix are distributed to polls/urls to look for the corresponding view.

Next run server, Access http://127.0.0.1:8000/polls/can see the following-that is, what we wrote in views.py

Hello World
Summarize

The main thing is that we basically completed a simple Hello world with some of the commands provided by Django, and we talked about the Django directory structure, which simply involves some view and URL notation.

Code location

Http://pan.baidu.com/s/1o84GjVk

Frist Django app-First, create a project

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.