Never too late to learn.

0%

Django Tutorial---Writing your first Django app

Meet Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

安装虚拟环境

安装virtualenv

$ pip install virtualenv

virtualenvwrapper 提供了一系列命令使得和虚拟环境工作变得愉快许多。它把你所有的虚拟环境都放在一个地方。

$ pip install virtualenvwrapper

windows下使用下面命令安装

$ pip install virtualenvwrapper-win

在Windows中,WORKON_HOME默认的路径是 %USERPROFILE%Envs 。

创建一个虚拟环境

$ mkvirtualenv venv

这会在 ~/Envs或者%USERPROFILE%Envs(windows中)中创建 venv 文件夹。

$ workon venv

在虚拟环境上工作

$ pip list

查看该环境下的python库

$ deactivate

退出虚拟环境

安装django

$ pip install django

查看安装版本

$ python -m django –version

创建一个Django工程

$ django-admin startproject mysite

试运行Django自带的一个轻量级Web server

$ python manage.py runserver [port]

port: 指定端口号, 默认8000

创建Polls应用,将会新建一个polls目录

$ python manage.py startapp polls

在polls应用下编写第一个view,

1
2
3
4
5
6
7
8
polls/views.py


from django.http import HttpResponse


def index(request):
return HttpResponse("Hello, you're at the polls index")

polls目录下创建一个urls.py文件,编写URLconf

1
2
3
4
5
6
7
8
9
polls/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
]

将root的URLconf指向polls应用中的urls模型

1
2
3
4
5
6
7
8
9
mysite/urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]

数据库迁移

在mysite/settings.py配置数据库信息,执行

$ python manage.py migrate

创建模型

1
2
3
4
5
6
7
8
9
10
11
12
13
polls/models.py
from django.db import models


class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')


class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

激活模型

mysite/settings.pyINSTALLED_APPS中添加路径

‘polls.apps.PollsConfig’

The PollsConfig class is in the polls/apps.py file, so its dotted path is ‘polls.apps.PollsConfig’

运行命令:

python manage.py makemigrations [polls]

python manage.py migrate

Play with the API

python manage.py shell

Django Admin

创建一个超级用户用于登录网站后台

$ python manage.py creatsuperuser

然后runserver,登录http://127.0.0.1:8000/admin/

Make the poll app modifiable in the admin

1
2
3
4
5
6
polls/admin.py
from django.contrib import admin

from .models import Question

admin.site.register(Question)

编写实用的views

在polls目录中创建templates目录,然后在templates目录下创建与应用名相同的目录polls,并在polls中创建文件index.html。总之,模板文件的路径应该是polls/templates/polls/index.html,

1
2
3
4
5
6
7
8
9
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

在polls目录中创建static目录,Django会在这里寻找静态文件,然后在static目录下创建polls目录,并在该目录下创建文件style.css

1
2
3
4
5
6
7
8
9
polls/static/polls/style.css

li a {
color: green;
}

li a {
color: green;
}
Coffee? ☕