django最新版本是多少(Python2.7安装Django报错)

本文目录
Python2.7安装Django报错
解决方法:django现在的版本不支持2.7的,你在下载的django时后面加==1.11
代表我需要下载1.11版本的django
ps:如果是刚学python的话建议不要看所有2.0版本的教程,这些都已经要被淘汰了
如何创建一个Django网站
本文演示如何创建一个简单的 django 网站,使用的 django 版本为1.7。
1. 创建项目
运行下面命令就可以创建一个 django 项目,项目名称叫 mysite :
$ django-admin.py startproject mysite
创建后的项目目录如下:
mysite
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
说明:
__init__.py :让 Python 把该目录当成一个开发包 (即一组模块)所需的文件。 这是一个空文件,一般你不需要修改它。
manage.py :一种命令行工具,允许你以多种方式与该 Django 项目进行交互。 键入python manage.py help,看一下它能做什么。 你应当不需要这个文件;在这个目录下生成它纯是为了方便。
settings.py :该 Django 项目的设置或配置。
urls.py:Django项目的URL路由设置。目前,它是空的。
wsgi.py:WSGI web 应用服务器的配置文件。更多细节,查看 How to deploy with WSGI
接下来,你可以修改 settings.py 文件,例如:修改 LANGUAGE_CODE、设置时区 TIME_ZONE
SITE_ID = 1
LANGUAGE_CODE = ’zh_CN’
TIME_ZONE = ’Asia/Shanghai’
USE_TZ = True
***隐藏网址***
$ sudo pip install pytz
2. 运行项目
在运行项目之前,我们需要创建数据库和表结构,这里我使用的默认数据库:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying sessions.0001_initial... OK
然后启动服务:
$ python manage.py runserver
你会看到下面的输出:
Performing system checks...
System check identified no issues (0 silenced).
January 28, 2015 - 02:08:33
Django version 1.7.1, using settings ’mysite.settings’
***隐藏网址***
Quit the server with CONTROL-C.
***隐藏网址***
你也可以指定启动端口:
$ python manage.py runserver 8080
以及指定 ip:
$ python manage.py runserver 0.0.0.0:8000
3. 创建 app
前面创建了一个项目并且成功运行,现在来创建一个 app,一个 app 相当于项目的一个子模块。
在项目目录下创建一个 app:
$ python manage.py startapp polls
如果操作成功,你会在 mysite 文件夹下看到已经多了一个叫 polls 的文件夹,目录结构如下:
polls
├── __init__.py
├── admin.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
1 directory, 6 files
4. 创建模型
每一个 Django Model 都继承自 django.db.models.Model
在 Model 当中每一个属性 attribute 都代表一个 database field
通过 Django Model API 可以执行数据库的增删改查, 而不需要写一些数据库的查询语句
打开 polls 文件夹下的 models.py 文件。创建两个模型:
import datetime
from django.db import models
from django.utils import timezone
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField(’date published’)
def was_published_recently(self):
return self.pub_date 》= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
然后在 mysite/settings.py 中修改 INSTALLED_APPS 添加 polls:
INSTALLED_APPS = (
’django.contrib.admin’,
’django.contrib.auth’,
’django.contrib.contenttypes’,
’django.contrib.sessions’,
’django.contrib.messages’,
’django.contrib.staticfiles’,
’polls’,
)
在添加了新的 app 之后,我们需要运行下面命令告诉 Django 你的模型做了改变,需要迁移数据库:
$ python manage.py makemigrations polls
你会看到下面的输出日志:
Migrations for ’polls’:
0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice
你可以从 polls/migrations/0001_initial.py 查看迁移语句。
运行下面语句,你可以查看迁移的 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", "votes", "id") SELECT "choice_text", "votes", "id" 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");
COMMIT;
你可以运行下面命令,来检查数据库是否有问题:
$ python manage.py check
再次运行下面的命令,来创建新添加的模型:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, polls, auth, sessions
Running migrations:
Applying polls.0001_initial... OK
总结一下,当修改一个模型时,需要做以下几个步骤:
修改 models.py 文件
运行 python manage.py makemigrations 创建迁移语句
运行 python manage.py migrate,将模型的改变迁移到数据库中
你可以阅读 django-admin.py documentation,查看更多 manage.py 的用法。
创建了模型之后,我们可以通过 Django 提供的 API 来做测试。运行下面命令可以进入到 python shell 的交互模式:
$ python manage.py shell
下面是一些测试:
》》》 from polls.models import Question, Choice # Import the model classes we just wrote.
# No questions are in the system yet.
》》》 Question.objects.all()
# Create a new Question.
# Support for time zones is enabled in the default settings file, so
# Django expects a datetime with tzinfo for pub_date. Use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
》》》 from django.utils import timezone
》》》 q = Question(question_text="What’s new?", pub_date=timezone.now())
# Save the object into the database. You have to call save() explicitly.
》》》 q.save()
# Now it has an ID. Note that this might say "1L" instead of "1", depending
# on which database you’re using. That’s no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
》》》 q.id
1
# Access model field values via Python attributes.
》》》 q.question_text
"What’s new?"
》》》 q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=《UTC》)
# Change values by changing the attributes, then calling save().
》》》 q.question_text = "What’s up?"
》》》 q.save()
# objects.all() displays all the questions in the database.
》》》 Question.objects.all()
打印所有的 Question 时,输出的结果是 ,我们可以修改模型类,使其输出更为易懂的描述。修改模型类:
from django.db import models
class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text
class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text
接下来继续测试:
》》》 from polls.models import Question, Choice
# Make sure our __str__() addition worked.
》》》 Question.objects.all()
# Django provides a rich database lookup API that’s entirely driven by
# keyword arguments.
》》》 Question.objects.filter(id=1)
》》》 Question.objects.filter(question_text__startswith=’What’)
# Get the question that was published this year.
》》》 from django.utils import timezone
》》》 current_year = timezone.now().year
》》》 Question.objects.get(pub_date__year=current_year)
《Question: What’s up?》
# Request an ID that doesn’t exist, this will raise an exception.
》》》 Question.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Question matching query does not exist.
# Lookup by a primary key is the most common case, so Django provides a
# shortcut for primary-key exact lookups.
# The following is identical to Question.objects.get(id=1).
》》》 Question.objects.get(pk=1)
《Question: What’s up?》
# Make sure our custom method worked.
》》》 q = Question.objects.get(pk=1)
# Give the Question a couple of Choices. The create call constructs a new
# Choice object, does the INSERT statement, adds the choice to the set
# of available choices and returns the new Choice object. Django creates
# a set to hold the "other side" of a ForeignKey relation
# (e.g. a question’s choice) which can be accessed via the API.
》》》 q = Question.objects.get(pk=1)
# Display any choices from the related object set -- none so far.
》》》 q.choice_set.all()
# Create three choices.
》》》 q.choice_set.create(choice_text=’Not much’, votes=0)
《Choice: Not much》
》》》 q.choice_set.create(choice_text=’The sky’, votes=0)
《Choice: The sky》
》》》 c = q.choice_set.create(choice_text=’Just hacking again’, votes=0)
# Choice objects have API access to their related Question objects.
》》》 c.question
《Question: What’s up?》
# And vice versa: Question objects get access to Choice objects.
》》》 q.choice_set.all()
》》》 q.choice_set.count()
3
# The API automatically follows relationships as far as you need.
# Use double underscores to separate relationships.
# This works as many levels deep as you want; there’s no limit.
# Find all Choices for any question whose pub_date is in this year
# (reusing the ’current_year’ variable we created above).
》》》 Choice.objects.filter(question__pub_date__year=current_year)
# Let’s delete one of the choices. Use delete() for that.
》》》 c = q.choice_set.filter(choice_text__startswith=’Just hacking’)
》》》 c.delete()
》》》
上面这部分测试,涉及到 django orm 相关的知识,详细说明可以参考 Django中的ORM。
5. 管理 admin
Django有一个优秀的特性, 内置了Django admin后台管理界面, 方便管理者进行添加和删除网站的内容.
新建的项目系统已经为我们设置好了后台管理功能,见 mysite/settings.py:
INSTALLED_APPS = (
’django.contrib.admin’, #默认添加后台管理功能
’django.contrib.auth’,
’django.contrib.contenttypes’,
’django.contrib.sessions’,
’django.contrib.messages’,
’django.contrib.staticfiles’,
’mysite’,
)
同时也已经添加了进入后台管理的 url, 可以在 mysite/urls.py 中查看:
url(r’^admin/’, include(admin.site.urls)), #可以使用设置好的url进入网站后台
接下来我们需要创建一个管理用户来登录 admin 后台管理界面:
$ python manage.py createsuperuser
Username (leave blank to use ’june’): admin
Email address:
Password:
Password (again):
Superuser created successfully.
总结
最后,来看项目目录结构:
mysite
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
├── polls
│ ├── __init__.py
│ ├── admin.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ ├── models.py
│ ├── templates
│ │ └── polls
│ │ ├── detail.html
│ │ ├── index.html
│ │ └── results.html
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
└── templates
└── admin
└── base_site.htm
通过上面的介绍,对 django 的安装、运行以及如何创建视 图和模型有了一个清晰的认识,接下来就可以深入的学习 django 的自动化测试、持久化、中间件、国 际 化等知识。
manage在python里什么意思
1. 安装 Python
在命令行输入 python -v ,如果输出了 Python 的版本号,说明 Python 已安装成功。
》python -V
Python 3.7.3
2. 安装Django
pip install django
》Python
》》》 import django
》》》 print(django.get_version())
2.2.1
3. 建立 Django 工程
》django-admin startproject blogproject
进入工程所在目录 D:\(你可能设置在其它路径),会发现多了一个 blogproject\ 的目录,其内部的文件结构如下:
blogproject\
manage.py
blogproject\
__init__.py
settings.py
urls.py
wsgi.py
最顶层的 blogproject\ 目录是我们刚刚指定的工程目录。blogproject\ 目录下面有一个 manage.py 文件,manage 是管理的意思,顾名思义 manage.py 就是 Django 为我们生成的管理这个项目的 Python 脚本文件,以后用到时会再次介绍。与 manage.py 同级的还有一个 blogproject\ 的目录,这里面存放了一些 Django 的配置文件,例如 settings.py、urls.py 等等,以后用到时会详细介绍。
Hello Django
网站需要运行在一个 Web 服务器上,Django 已经为我们提供了一个用于本地开发的 Web 服务器。在命令行工具里进入到 manage.py 所在目录,即最外层的 blogproject\ 目录下。运行 python manage.py runserver 命令就可以在本机上开启一个 Web 服务器:
怎么查看django版本
在windows下面启动cmd命令行。
在linux直接使用终端。 然后调用python解释器。
python 出现》》》后输入下列语句查看django版本,如果没有安装django,那么当你import django就会出错。
》》》 import django
》》》 print django.VERSION
下面是我windows下面的输出:
(1, 3, 1, ’final’, 0) 完。

更多文章:
java初级编程经典题(一套JAVA的初级题目,跪求各位大虾帮帮忙)
2025年10月27日 05:45
一个完整的docker服务包括(Docker的主要作用是什么_docker属于什么)
2026年9月9日 08:45
python第三方库(怎么看安装了哪些第三方库在python)
2026年5月23日 18:30
python中row函数是什么意思(python中的row具体有什么用呢)
2025年8月25日 18:15
true和false的来源(false什么意思 解析false的含义和用法)
2026年2月15日 20:15
用Wps表格制作一张方便的报价单模板?怎么把一个EXCEL表格变成模板
2026年8月6日 14:00
analyze翻译(具备独立的思辨和分析能力的翻译是:什么意思)
2026年1月16日 10:15
intellijidea如何升级(intellij idea 怎么让class自动更新到war)
2026年3月23日 11:00
log4j2 xml配置(log4j2.xml怎么调用properties)
2025年6月7日 16:45
网页制作滚动字幕代码(写出制作滚动字幕的代码(包括方向、速度、行为、区域、区域背景等))
2025年6月23日 22:30
婚纱摄影网站图片(哪些地方适合拍婚纱照,这些外景都是很漂亮的)
2026年8月23日 06:45
c语言考试题及答案2018(2018年9月计算机二级C语言程序设计章节习题4)
2026年2月25日 15:15










