Django环境搭建及安装

最近因为项目需要,开始研究django,下面分享我的使用过程,希望对大家有用

官网:

教程网址

安装:

sudo apt-get install python-django
or
sudo pip install django

安装有效性检查


django-admin --version

创建项目

django-admin startproject mysite

目录结构

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

启动服务

python manage.py runserver

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run ‘python manage.py migrate‘ to apply them.

April 21, 2016 - 07:32:20
Django version 1.9.5, using settings ‘mysite.settings‘
Starting development server at http://127.0.0.1:8000/

目录结构


├── db.sqlite3
├── manage.py
└── mysite
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    ├── urls.pyc
    ├── wsgi.py
    └── wsgi.pyc

可见只多了 db.sqlite3文件及一些pyc文件

这个数据库文件的名字是在mysite/settings.py指定的


DATABASES = {
    ‘default‘: {
        ‘ENGINE‘: ‘django.db.backends.sqlite3‘,
        ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
    }
}

这里默认是sqlite3数据库引擎,有需要可以更改引擎比如mysql等等

我们来看下这个数据库生成了什么?

sqlite3 db.sqlite3


SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL);
COMMIT;
sqlite>

可以看出只是创建了一个表:django_migrations

创建应用

python manage.py startapp local_db

我们来看看生成了哪些文件:


├── db.sqlite3
├── local_db
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── mysite
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    ├── urls.pyc
    ├── wsgi.py
    └── wsgi.pyc

可见生成了local_db目录


jivin@jivin-ThinkPad-T450:~/back/experiment/django/mysite/local_db$ cat admin.py
from django.contrib import admin

# Register your models here.

jivin@jivin-ThinkPad-T450:~/back/experiment/django/mysite/local_db$ cat apps.py
from __future__ import unicode_literals

from django.apps import AppConfig

class LocalDbConfig(AppConfig):
    name = ‘local_db‘

jivin@jivin-ThinkPad-T450:~/back/experiment/django/mysite/local_db$ cat models.py
from __future__ import unicode_literals

from django.db import models

# Create your models here.

jivin@jivin-ThinkPad-T450:~/back/experiment/django/mysite/local_db$ cat views.py
from django.shortcuts import render

# Create your views here.

jivin@jivin-ThinkPad-T450:~/back/experiment/django/mysite/local_db$ cat tests.py
from django.test import TestCase

# Create your tests here.

写第一个view

修改local_db/views.py


from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You‘re at the local_db index.")

将url映射到这个函数

新增local_db/urls.py


from django.conf.urls import url

from . import views

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

增加urls文件

cat mysite/urls.py


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

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

重新执行server:python manage.py runserver

然后在浏览器输入:http://127.0.0.1:8000/local_db/

migrate

每次新增一个应用时,需要创建他们需要的数据库,这时就需要先执行这个

python manage.py migrate

Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying sessions.0001_initial... OK
[email protected]-ThinkPad-T450:~/back/experiment/django/mysite$ sqlite3 db.sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL);
INSERT INTO "django_migrations" VALUES(1,‘contenttypes‘,‘0001_initial‘,‘2016-04-21 08:01:35.838744‘);
INSERT INTO "django_migrations" VALUES(2,‘auth‘,‘0001_initial‘,‘2016-04-21 08:01:35.904882‘);
INSERT INTO "django_migrations" VALUES(3,‘admin‘,‘0001_initial‘,‘2016-04-21 08:01:35.952628‘);
INSERT INTO "django_migrations" VALUES(4,‘admin‘,‘0002_logentry_remove_auto_add‘,‘2016-04-21 08:01:36.006238‘);
INSERT INTO "django_migrations" VALUES(5,‘contenttypes‘,‘0002_remove_content_type_name‘,‘2016-04-21 08:01:36.087153‘);
INSERT INTO "django_migrations" VALUES(6,‘auth‘,‘0002_alter_permission_name_max_length‘,‘2016-04-21 08:01:36.149247‘);
INSERT INTO "django_migrations" VALUES(7,‘auth‘,‘0003_alter_user_email_max_length‘,‘2016-04-21 08:01:36.218252‘);
INSERT INTO "django_migrations" VALUES(8,‘auth‘,‘0004_alter_user_username_opts‘,‘2016-04-21 08:01:36.275377‘);
INSERT INTO "django_migrations" VALUES(9,‘auth‘,‘0005_alter_user_last_login_null‘,‘2016-04-21 08:01:36.334689‘);
INSERT INTO "django_migrations" VALUES(10,‘auth‘,‘0006_require_contenttypes_0002‘,‘2016-04-21 08:01:36.357559‘);
INSERT INTO "django_migrations" VALUES(11,‘auth‘,‘0007_alter_validators_add_error_messages‘,‘2016-04-21 08:01:36.414787‘);
INSERT INTO "django_migrations" VALUES(12,‘sessions‘,‘0001_initial‘,‘2016-04-21 08:01:36.459969‘);
CREATE TABLE "auth_group" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(80) NOT NULL UNIQUE);
CREATE TABLE "auth_group_permissions" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "group_id" integer NOT NULL REFERENCES "auth_group" ("id"), "permission_id" integer NOT NULL REFERENCES "auth_permission" ("id"));
CREATE TABLE "auth_user_groups" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "user_id" integer NOT NULL REFERENCES "auth_user" ("id"), "group_id" integer NOT NULL REFERENCES "auth_group" ("id"));
CREATE TABLE "auth_user_user_permissions" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "user_id" integer NOT NULL REFERENCES "auth_user" ("id"), "permission_id" integer NOT NULL REFERENCES "auth_permission" ("id"));
CREATE TABLE "django_admin_log" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "object_id" text NULL, "object_repr" varchar(200) NOT NULL, "action_flag" smallint unsigned NOT NULL, "change_message" text NOT NULL, "content_type_id" integer NULL REFERENCES "django_content_type" ("id"), "user_id" integer NOT NULL REFERENCES "auth_user" ("id"), "action_time" datetime NOT NULL);
CREATE TABLE "django_content_type" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app_label" varchar(100) NOT NULL, "model" varchar(100) NOT NULL);
INSERT INTO "django_content_type" VALUES(1,‘admin‘,‘logentry‘);
INSERT INTO "django_content_type" VALUES(2,‘auth‘,‘permission‘);
INSERT INTO "django_content_type" VALUES(3,‘auth‘,‘group‘);
INSERT INTO "django_content_type" VALUES(4,‘auth‘,‘user‘);
INSERT INTO "django_content_type" VALUES(5,‘contenttypes‘,‘contenttype‘);
INSERT INTO "django_content_type" VALUES(6,‘sessions‘,‘session‘);
CREATE TABLE "auth_permission" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "content_type_id" integer NOT NULL REFERENCES "django_content_type" ("id"), "codename" varchar(100) NOT NULL, "name" varchar(255) NOT NULL);
INSERT INTO "auth_permission" VALUES(1,1,‘add_logentry‘,‘Can add log entry‘);
INSERT INTO "auth_permission" VALUES(2,1,‘change_logentry‘,‘Can change log entry‘);
INSERT INTO "auth_permission" VALUES(3,1,‘delete_logentry‘,‘Can delete log entry‘);
INSERT INTO "auth_permission" VALUES(4,2,‘add_permission‘,‘Can add permission‘);
INSERT INTO "auth_permission" VALUES(5,2,‘change_permission‘,‘Can change permission‘);
INSERT INTO "auth_permission" VALUES(6,2,‘delete_permission‘,‘Can delete permission‘);
INSERT INTO "auth_permission" VALUES(7,3,‘add_group‘,‘Can add group‘);
INSERT INTO "auth_permission" VALUES(8,3,‘change_group‘,‘Can change group‘);
INSERT INTO "auth_permission" VALUES(9,3,‘delete_group‘,‘Can delete group‘);
INSERT INTO "auth_permission" VALUES(10,4,‘add_user‘,‘Can add user‘);
INSERT INTO "auth_permission" VALUES(11,4,‘change_user‘,‘Can change user‘);
INSERT INTO "auth_permission" VALUES(12,4,‘delete_user‘,‘Can delete user‘);
INSERT INTO "auth_permission" VALUES(13,5,‘add_contenttype‘,‘Can add content type‘);
INSERT INTO "auth_permission" VALUES(14,5,‘change_contenttype‘,‘Can change content type‘);
INSERT INTO "auth_permission" VALUES(15,5,‘delete_contenttype‘,‘Can delete content type‘);
INSERT INTO "auth_permission" VALUES(16,6,‘add_session‘,‘Can add session‘);
INSERT INTO "auth_permission" VALUES(17,6,‘change_session‘,‘Can change session‘);
INSERT INTO "auth_permission" VALUES(18,6,‘delete_session‘,‘Can delete session‘);
CREATE TABLE "auth_user" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "password" varchar(128) NOT NULL, "last_login" datetime NULL, "is_superuser" bool NOT NULL, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL, "email" varchar(254) NOT NULL, "is_staff" bool NOT NULL, "is_active" bool NOT NULL, "date_joined" datetime NOT NULL, "username" varchar(30) NOT NULL UNIQUE);
CREATE TABLE "django_session" ("session_key" varchar(40) NOT NULL PRIMARY KEY, "session_data" text NOT NULL, "expire_date" datetime NOT NULL);
DELETE FROM sqlite_sequence;
INSERT INTO "sqlite_sequence" VALUES(‘django_migrations‘,12);
INSERT INTO "sqlite_sequence" VALUES(‘django_admin_log‘,0);
INSERT INTO "sqlite_sequence" VALUES(‘django_content_type‘,6);
INSERT INTO "sqlite_sequence" VALUES(‘auth_permission‘,18);
INSERT INTO "sqlite_sequence" VALUES(‘auth_user‘,0);
CREATE UNIQUE INDEX "auth_group_permissions_group_id_0cd325b0_uniq" ON "auth_group_permissions" ("group_id", "permission_id");
CREATE INDEX "auth_group_permissions_0e939a4f" ON "auth_group_permissions" ("group_id");
CREATE INDEX "auth_group_permissions_8373b171" ON "auth_group_permissions" ("permission_id");
CREATE UNIQUE INDEX "auth_user_groups_user_id_94350c0c_uniq" ON "auth_user_groups" ("user_id", "group_id");
CREATE INDEX "auth_user_groups_e8701ad4" ON "auth_user_groups" ("user_id");
CREATE INDEX "auth_user_groups_0e939a4f" ON "auth_user_groups" ("group_id");
CREATE UNIQUE INDEX "auth_user_user_permissions_user_id_14a6b632_uniq" ON "auth_user_user_permissions" ("user_id", "permission_id");
CREATE INDEX "auth_user_user_permissions_e8701ad4" ON "auth_user_user_permissions" ("user_id");
CREATE INDEX "auth_user_user_permissions_8373b171" ON "auth_user_user_permissions" ("permission_id");
CREATE INDEX "django_admin_log_417f1b1c" ON "django_admin_log" ("content_type_id");
CREATE INDEX "django_admin_log_e8701ad4" ON "django_admin_log" ("user_id");
CREATE UNIQUE INDEX "django_content_type_app_label_76bd3d3b_uniq" ON "django_content_type" ("app_label", "model");
CREATE UNIQUE INDEX "auth_permission_content_type_id_01ab375a_uniq" ON "auth_permission" ("content_type_id", "codename");
CREATE INDEX "auth_permission_417f1b1c" ON "auth_permission" ("content_type_id");
CREATE INDEX "django_session_de54fa62" ON "django_session" ("expire_date");
COMMIT;

新增模型

修改local_db/models.py


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.py让其生效:


INSTALLED_APPS = [
    ‘local_db.apps.LocalDbConfig‘,
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
]

生成model对应的migrate:

python manage.py makemigrations local_db


Migrations for ‘local_db‘:
  0001_initial.py:
    - Create model Choice
    - Create model Question
    - Add field question to choice

这条命令就是为了生成local_db/0001_initial.py

然后我们看对应sqlmigrate的动作

python manage.py sqlmigrate local_db 0001

这个0001对应前面local_db/migrations/0001_initial.py文件名前缀


BEGIN;
--
-- Create model Choice
--
CREATE TABLE "local_db_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
--
-- Create model Question
--
CREATE TABLE "local_db_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE "local_db_choice" RENAME TO "local_db_choice__old";

下面真正生成model对应的数据库表:

python manage.py migrate local_db


Operations to perform:
  Apply all migrations: local_db
Running migrations:
  Rendering model states... DONE
  Applying local_db.0001_initial... OK

sqlite3 db.sqlite3


CREATE TABLE "local_db_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
CREATE TABLE "local_db_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "local_db_question" ("id"));


/**********************

* 本文来自博客 “爱踢门”

* 转载请标明出处:http://blog.csdn.net/itleaks

********************************/

时间: 2024-11-11 05:39:05

Django环境搭建及安装的相关文章

Django环境搭建(二)

1.创建应用apitest,项目目录打开命令窗口输入:python  manage.py  startapp  apitest(创建应用命令--常用),如下图: 将apitest添加到autotest项目下面,在settings.py加入'apitest',如下图: 2.创建视图,在apitest目录下的views.py文件中加入test函数,如下图: 3.创建映射,把视图当前的函数结果映射到前端浏览器页面,在autotest目录下的urls.py文件下加入如下内容: 在浏览器中输入127.0.

HHvm Apache 2.4 Nginx建站环境搭建方法安装运行WordPress博客

HHvm Apache 2.4 Nginx建站环境搭建方法安装运行WordPress博客 VPS主机   2014年06月02日 17:20 评论» 文章目录 Debian上安装 CentOS上安装 Ubuntu上安装 装Wordpress HHvm安装小结 HHVM是一个强大的可以用于替代PHP的建站环境,出自Facebook团队,它是一个PHP的JIT(Just-In- Time)编译器,同时具有产生快速代码和即时编译的优点.实践证明HHVM可以明显地提升PHP执行速度,节省大量的服务器资源

python+django环境搭建

前些天看到小阳买回来的一本 Python 基础教程,于是就产生了使用 Python 来开发一个小博客的想法.这个 Python 网站开发系列文章将不会过多说Python 基本语法,主要是面向实践的.使用 Python 2.7.框架为 Django 1.3.数据库为 Sql Server(没有用过 MySql). 当然,使用任何一门语言,如果对基本语法不了解的话,开发肯定会显得无从下手.在这里提供一些 Python.Django 方面的电子书资料,这些电子书包括:<征服Python:语言基础与典型

Django环境搭建及学前准备

本周的成就在于搭建了两台Linux系统,其中一台为UbuntuKylin,另一台为树莓派基于Debian的操作系统. Ubuntu安装到淘汰下来的主机上,DDR2 1G内存,双核处理器,Kylin太花俏,没怎么使用.主要还是用树莓派在学习. 树莓派用16G的TF卡做硬盘,安装了xrdp,可以用办公电脑mstsc可进行远程控制,用putty比较多,正在努力熟悉命令行,用Vim写代码. 另外还用之前买的花生棒做端口映射,免费的服务比较少功能,只允许映射两个端口,都用在了树莓派上,80用来建站,22用

Windows下Django环境搭建

总体示意图如下:  Windows下搭建Django环境 1.安装Python版本 2.安装pip工具,一般Python安装都会自动会有这个,在你python安装命令下Scripts文件夹下 3.django,第一种方法是通过pip安装,在windows 7下只要pip install django就装好了.第二种方法是也通过源码安装,git clone https://github.com/django/django.git下载源码:然后通过python setup.py install安装.

Django环境搭建(一)

搭建Django环境之前先搭建python运行环境 需要了解: 解释器(编译器): 计算机不能直接理解任何除机器语言外的其他语言,所以程序员必须要把自己写的语言翻译成机器语言,而将其他语言翻译成机器语言的工具,就叫做编译器 编译器翻译的方式有两种,一个是编译,一个是解释,两者的区别在于翻译时间点的不同.在编译器以解释的方式运行的时候,称之为解释器 Django: Django是一个开放源代码的Web应用框架,由解释型高级语言Python写成.采用了MVC的框架模式,即模型M,视图V和控制器C.

Node.js环境搭建&amp;&amp;npm安装

Node.js环境搭建 第一步:进入官网,然后直接点击左边的下载左边的部分,因为其提示 “Recommended For Most Users”. 第二步:下载得到的是msi文件,直接双击安装,一路next即可安装完成. 第三步:这时,我们就可以在“开始”中找到最新添加了两项内容,可以创建快捷方式到桌面上方便使用.如图: 第四步:安装完成后,按下win+R ,然后输入cmd后进入dos窗口,直接输入 node -v,这时我们可以看到你所安装的版本号,表示安装成功,如下所示: 第五步:npm安装.

Vue.js开发环境搭建(安装过程遇到的问题)

网络上关于Vue.js的安装过程有很多介绍,这里不多赘述,这里只是记录自己安装过程和遇到的问题 1 首先安装node.js 到官网下载node.js安装包,安装过程和一般软件没有区别.安装完成后到终端查看版本号,若如下图所示,表示安装成功. 2.node.js环境变量新建 其实安装完node,就自动在path里增加环境变量,但是为了以后的本地部署项目,我们需要找到node.js的安装根目录,在当中新建"node_global"和"node_cache"两个文件夹.

appium使用教程(一 环境搭建)-------------2.安装部署

1)      安装appium     2)      SDK(不建议使用模拟器,占用内存很大,会造成机器卡顿.另外模拟器还存在兼容性问题:pc为x86架构,绝大多数的手机都是ARM架构,app的兼容性会有问题) 官方下载地址https://android-sdk.en.softonic.com/ 安装完成后截图:   安装模拟器(使用真机跳过这一步)     启动模拟器(使用真机跳过这一步)     先新建模拟器,再启动模拟器 3)      检查appium环境 a.进入D:\Progra