创建一个django项目

提示:django的项目目录不能放在web服务其的目录中,也有可能会被被人下载下源码来

创建一个名为first的django项目

$ django-admin startproject first

创建后生成的目录及文件

first/
    manage.py
    first/
    __init__.py
    settings.py
    urls.py
    wsgi.py

这些文件及目录的作用:
外层first 仅仅是一个项目容器
manage.py 一个命令行工具
内层的first是项目的正真python包
first/init.py 告诉python这个目录应该看作一个python包
first/settings.py 该项目的设置
first/urls.py 项目的url声明
first/wsgi.py usgi的入口

首先创建数据库
django默认的数据库是sqlite3,我喜欢使用mysql接下来就配置一下mysql数据库吧
以下是mysql数据库的配置(参考链接

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'first',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

创建数据库表

$ python manage.py migrate

执行有可能报错,缺少MySQLdb包,这个包是第三方包,需要自己下载安装。
成功的执行这个命令之后就可以看到这个数据库中已经创建了这些表了
+—————————-+
| Tables_in_first |
+—————————-+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+—————————-+

启动开发服务器

$ python manage.py runserver 

默认监听127.0.0.1:8000

$ python manage.py runserver 0.0.0.0:8000

监听任何ip的8000端口的数据

创建一个应用

$ python manage.py startapp polls

这里将创建一个目录

polls/
    __init__.py
    admin.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

模型指出了数据的唯一来源遵循DRY原则
先创建两个模型Question 和Choice
Question有一个问题属性(question_text)和一个发布时间属性(publish_data)
Choice有两个字段选择的内容和选择的得票统计
在settings文件中的INSTALLED_APPS中增加新建的polls项目

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

将你更改的model存储为迁移文件

$ python manage.py makemigrations polls

查看迁移行为将会执行哪些sql

$ python manage.py sqlmigrate polls 0001

输出:

BEGIN;
CREATE TABLE `polls_choice` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `choice_text` varchar(200) NOT NULL, `votes` integer NOT NULL);
CREATE TABLE `polls_question` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `question_text` varchar(200) NOT NULL, `pub_date` datetime NOT NULL);
ALTER TABLE `polls_choice` ADD COLUMN `question_id` integer NOT NULL;
ALTER TABLE `polls_choice` ALTER COLUMN `question_id` DROP DEFAULT;
CREATE INDEX `polls_choice_7aa0f6ee` ON `polls_choice` (`question_id`);
ALTER TABLE `polls_choice` ADD CONSTRAINT `polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id` FOREIGN KEY (`question_id`) REFERENCES `polls_question` (`id`);

COMMIT;

模型API

(python2.7) [root@madison first]# python manage.py shell
Python 2.7.13 (default, Apr 19 2017, 17:45:11) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> django.setup()
>>> 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())
  File "<console>", line 1
    q = Question(question_text='What's new?',pub_date=timezone.now())
                                     ^
SyntaxError: invalid syntax
>>> q = Question(question_text="What's new?",pub_date=timezone.now())
>>> q.save()
>>> q.question_text()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'str' object is not callable
>>> q.question_text
"What's new?"
>>> q.question_text = "What's up?"
>>> q.save()
>>> Question.objects.all()
[<Question: Question object>]
>>> Question.objects.all()
[<Question: Question object>]
>>> q.id
1L

[\<\Question: Question object>]是没有意义的

需要重写model里的__str__和unicode方法,返回标记某条记录的有用的信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值