Build Python's Django framework environment and set up and run the first app tutorial _python

Source: Internet
Author: User
Tags auth sessions install django pip install django in python

Django is the most popular web framework in Python, so what is called the framework, which helps you build the overall architecture of the program, and all we need to do is fill in the logic, and the framework can invoke the logic you write at the right time, Instead of calling our own logic to make web development more agile.

Django is a high-level Python web framework that encourages rapid, concise, design-minded development. By using this framework, you can reduce a lot of development headaches and make you more focused on writing your own app without having to reinvent the wheel. Django is free and open source.
Django Features:

    • Completely free and open source code
    • Rapid and efficient development
    • Using the MTV architecture (familiar with web development should say MVC architecture)
    • Powerful scalability.
    • How Django Works

Django Installation
Install the latest version of the Django version

#安装最新版本的Django
$ pip Install Django 
#或者指定安装版本
pip install-v django==1.7.1

Project creation
now, let's start by creating a Django project called My_blog.
The instructions for creating the project are as follows:

$ django-admin.py Startproject My_blog

Now look at the file structure of the entire project:

$ Tree My_blog  #打印树形文件结构
My_blog
├──manage.py
└──my_blog
  ├──__init__.py
  ├──settings.py
  ├──urls.py


1 directory, 5 files

Create a Django App
the app in Django I think is a functional module, and other web frameworks may be very different, will not function in different apps, convenient code reuse.
Build a article App:

$ python manage.py Startapp article

Now let's look at the structure of the entire project.

──article
│  ├──__init__.py
│  ├──admin.py
│  ├──migrations
│  │  └──_ _init__.py
│  ├──models.py
│  ├──tests.py
│  └──views.py
├──db.sqlite3
├──manage.py
├──my_blog
  ├──__init__.py
  ├──__pycache__
  │  ├──__init__.cpython-34.pyc
  │  ├──settings.cpython-34.pyc
  │  ├──urls.cpython-34.pyc
  │  └──wsgi.cpython-34.pyc
  ├──settings.py
  ├──urls.py
  └──wsgi.py

and add a new app under my_blog/my_blog/setting.py

Installed_apps = (
  ... ')
  Article ', #这里填写的是app的名称
)

Running programs

$ python manage.py runserver  #启动Django中的开发服务器
#如果运行上面命令出现以下提示 you
have unapplied migrations, your app may is not work properly, until they.
Run ' python manage.py migrate ' to apply them.
#请先使用下面命令
python manage.py migrate
#输出如下信息
Operations to perform:
 Apply all migrations: ContentTypes, Sessions, admin, auth
Running migrations: applying contenttypes.0001_initial
 ... OK
 Applying auth.0001_initial ... OK
 Applying admin.0001_initial ... OK
 Applying sessions.0001_initial ... Ok

After the operation is successful, the following information is displayed

#重新运行启动Django中的开发服务器
$ python manage.py runserver

#运行成功显示如下信息
System Check identified no issues (0 silenced).
December, 2014-08:56:00
Django version 1.7.1, using Settings ' my_blog.settings '
starting development Server A T http://127.0.0.1:8000/
Quit the server with Control-c.

Now you can start the browser, enter Http://127.0.0.1:8000/, and when it appears

It means you've made your first move!
Order Comb:

Python manage.py <command> [options] #Django command python manange.py-h help document
django-admin.py Startproject my_ Blog #创建项目
python manage.py startapp article #创建app


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.