Tutorial on setting up Python's Django framework environment and building and running the first App

WBOY
Release: 2016-07-22 08:56:32
Original
1127 people have browsed it

Django is the most popular Web Framework in python. So what is a Framework? The framework can help you build the overall structure of the program, and all we need to do is fill in the logic, and the framework can be called at the right time. The logic you write does not require us to call the logic ourselves, making web development more agile.

Django is an advanced Python Web framework that encourages fast, concise, and programming-based development. By using this framework, you can reduce a lot of development troubles, allowing you to focus more on writing your own app without having to reinvent the wheel. Wheels. Django is free and open source.
Django features:

  • Completely free and open source code
  • Fast and efficient development
  • Use MTV architecture (those who are familiar with web development should say it is MVC architecture)
  • Powerful scalability.
  • How Django works

Django installation
Install the latest Django version

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

Project Creation
Now let’s get started, let’s create a Django project named my_blog.
The instructions to create a project are as follows:

$ django-admin.py startproject my_blog
Copy after login

Now take a look at the file structure of the entire project:

$ tree my_blog  #打印树形文件结构
Copy after login

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


1 directory, 5 files

Copy after login

Create Django app
I think the app in Django is just a functional module, which may be very different from other web frameworks. Put non-functional functions in different apps to facilitate code reuse.
Create an article app:

$ python manage.py startapp article
Copy after login

Now let’s take a look again 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
Copy after login

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

INSTALLED_APPS = (
  ...
  'article', #这里填写的是app的名称
)
Copy after login

Run the program

$ python manage.py runserver  #启动Django中的开发服务器
Copy after login

#如果运行上面命令出现以下提示
You have unapplied migrations; your app may not work properly until they are applied.
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
Copy after login

After successful operation, the following information will be displayed

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

Copy after login

#运行成功显示如下信息
System check identified no issues (0 silenced).
December 21, 2014 - 08:56:00
Django version 1.7.1, using settings 'my_blog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Copy after login

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

201672153618511.png (496×637)

It means you have successfully taken the first step!
Command sorting:

python manage.py <command> [options] #Django Command python manange.py -h帮助文档
django-admin.py startproject my_blog #创建项目
python manage.py startapp article #创建app
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!