第一个 Django Project开发

来源:互联网 发布:tplink tl wn725n mac 编辑:程序博客网 时间:2024/05/24 07:37

本篇文章是 官网https://docs.djangoproject.com/en/1.7/intro/tutorial01/” 的实践版本。由于原文有较多的解释成分以及用英语书写不便于快速进入Django的开发。所以才有本文。

http://dmyz.org/archives/110

Part 1. 环境搭建测试

如需转载请注明出处:http://blog.csdn.net/michael_kong_nju/article/details/42922931谢谢。

1. 环境

Ubuntu 13.10    # cat /etc/issue  进行查看

Python 3.3.2+   # python -V         进行查看

Django 1.7.3.       # python -c "import django; print(django.get_version())"      进行查看

http://blog.csdn.net/michael_kong_nju/article/details/42878651 在这篇文章中记录了,如何安装以及升级

如果python的版本有区别的话,比如2.x.x那么在语法上会有区别。

2.  创建一个工程

我们将工程放在 /opt/django_programming/下面。

# cd /opt/django_programming         # django-admin.py startproject mysite   %%跟官方教程一致,我们新建的project取名为 mysite.

然后我们就完成了一个mysite项目的创建。我们用 tree命令查看刚才创建的结构。

root@michaelpc:/opt/django_programming# tree.└── mysite    %%%%外部的这个mysite就是刚才我们project的名字。这里就是一个容器,用来存放project的一些内容。    ├── manage.py    └── mysite        ├── __init__.py        ├── settings.py    %%%% Django Project的配置文件。        ├── urls.py        └── wsgi.py

3. 创建数据库。

    注意这里面作为Django的快速入门,我们使用的是python自带的Sqlite3数据库。如果是实际的开发商业项目不主张这么开做。可以查看后面的博文:*************

# vim  mysite/settings.py           %%%%%%打开配置文件,找到DATABASE数组内容,对DATABASE不做修改。  DATABASES = {     default': {                  'ENGINE': 'django.db.backends.sqlite3',                 %%%%默认使用的sqlite3数据库。如果之后需要关联别的如mysql 只需要在这里进行处理。           'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),          %%名称         }      }   将TIME_ZONE修改为:   TIME_ZONE = 'Asia/Shanghai'. 保存退出之后执行: #  python manage.py migrate         

至此数据库建立完成。

4.  开启服务器

这里我们使用Django自带的服务器,只需要使用下面命令就可以开启:(因为此命令不能中断,所以建议开启新的终端来运行这个命令)

 #  python manage.py runserver    %%%%%%%%%也可有用这个命令:  python manage.py runserver 0.0.0.0:8000    指定ip和端口,这里使用默认

然后在服务器中打开http://127.0.0.1:8000/可以看到一个欢迎界面。

至此一个project已经完成了,即我们完成了一个project的架构,下面我们开始建立具体的 应用(app)

5.  创建APP

 这里创建 polls这个投票应用。 在与manage.py相同目录下执行下面命令:

  #  python manage.py startapp polls
然后我们发现在当前目录下新建了一个polls目录,用tree命令查看文件结构.之后我们修改 polls下面的models.py文件。简单来说models存储了我们对数据模式格式的定义

# vim polls/models.py
 然后在 # Create your models here 下面加入:

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)    choice_text = models.CharField(max_length=200)    votes = models.IntegerField(default=0)
      
保存退出。这里定义了python的两个类分别是 Question 以及 Choice

6. 激活polls的model

修改 mysite/setting.py中的 INSTALL_APPS数组

# vim mysite/setting.py
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在INSTALL_APPS 数组的后面加入 'polls',使得下次创建APP时候会将polls一起创建。</span>

INSTALLED_APPS = (    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'polls',)
保存退出,执行:

# python manage.py makemigrations polls

出现如下执行结果:

Migrations for 'polls':  0001_initial.py:    - Create model Question    - Create model Choice    - Add field question to choice
运行下面语句查看返回的sql语句。

#python manage.py sqlmigrate polls 0001
下面是我的终端输出的信息:

BEGIN;CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);CREATE TABLE "polls_choice__new" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id"));INSERT INTO "polls_choice__new" ("choice_text", "question_id", "id", "votes") SELECT "choice_text", NULL, "id", "votes" FROM "polls_choice";DROP TABLE "polls_choice";ALTER TABLE "polls_choice__new" RENAME TO "polls_choice";CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id");

运行下面语句使得生成数据表

# python manage.py migrate
输出如下信息,数据表创建成功。

root@michaelpc:/opt/django_programming/mysite# python manage.py migrateOperations to perform:  Apply all migrations: sessions, admin, contenttypes, polls, authRunning migrations:  Applying polls.0001_initial... OK
至此完成了polls模型的创建。

7. 下面我们尝试使用sqlite在django中的api.

# python manage.py shell  %%进入交互界面

依次:

>>>from polls.models import Question, Choice    %%导入我们刚才床家你的模型类>>>Question.objects.all()  

>>> from django.utils import timezone>>> q = Question(question_text="What's new?", pub_date=timezone.now()) %%添加数据>>> q.save()

如果没有出错表明ok,还有好多API可供使用,这里就不一一阐述,可以做参考官方网站的教程:

https://docs.djangoproject.com/en/1.7/intro/tutorial01/#writing-your-first-django-app-part-1

 至此我们完成了环境的搭建,并且创建了project以及app,下面我们具体的做开发。

Part2. 实例开发

0 0