“Django安装与配置”的版本间的差异
来自个人维基
(以“官网:https://www.djangoproject.com Django当前最新版本是 1.11.4,1.11系列也是 Django支持 python2.7的最后一个系列版本,安装也很简单...”为内容创建页面) |
小 (→配置) |
||
(未显示1个用户的4个中间版本) | |||
第1行: | 第1行: | ||
+ | |||
+ | ===安装=== | ||
官网:https://www.djangoproject.com | 官网:https://www.djangoproject.com | ||
第6行: | 第8行: | ||
C:\Python27\Scripts>pip install Django==1.11.4 | C:\Python27\Scripts>pip install Django==1.11.4 | ||
+ | |||
+ | 确认安装是否成功: | ||
+ | <pre> | ||
+ | C:\Python27\Scripts>python | ||
+ | Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 | ||
+ | Type "help", "copyright", "credits" or "license" for more information. | ||
+ | >>> import django | ||
+ | >>> print django.get_version() | ||
+ | 1.11.4 | ||
+ | </pre> | ||
+ | |||
+ | ===创建第一个project=== | ||
+ | 1、进入你计算放置这个 project的目录,cmd 运行: | ||
+ | django-admin startproject mysite | ||
+ | 如果提示找不到 django-admin,可把 C:\\Python27\Scripts加入系统环境路径中。 | ||
+ | |||
+ | 正常的话,会生成以下目录结构: | ||
+ | <pre> | ||
+ | mysite/ | ||
+ | manage.py | ||
+ | mysite/ | ||
+ | __init__.py | ||
+ | settings.py | ||
+ | urls.py | ||
+ | wsgi.py | ||
+ | </pre> | ||
+ | |||
+ | 2、运行 | ||
+ | python manage.py runserver | ||
+ | |||
+ | 通过 http://127.0.0.1:8000/ 应该难看到欢迎界面了 :) | ||
+ | |||
+ | 不过上面这仅是开发模式使用,要正式使用,需要在这个 project上创建 App. | ||
+ | |||
+ | ===创建app=== | ||
+ | 进入 manage.py所在目录,运行: | ||
+ | python manage.py startapp polls | ||
+ | |||
+ | 将生成以下目录结构: | ||
+ | <pre> | ||
+ | polls/ | ||
+ | __init__.py | ||
+ | admin.py | ||
+ | apps.py | ||
+ | migrations/ | ||
+ | __init__.py | ||
+ | models.py | ||
+ | tests.py | ||
+ | views.py | ||
+ | </pre> | ||
+ | |||
+ | ===编写view=== | ||
+ | 打开 polls/views.py,编辑进以下内容: | ||
+ | <pre class="prettyprint"> | ||
+ | from django.http import HttpResponse | ||
+ | |||
+ | |||
+ | def index(request): | ||
+ | return HttpResponse("Hello, world. You're at the polls index.") | ||
+ | </pre> | ||
+ | |||
+ | ===配置=== | ||
+ | 再创建一个url配置文件 urls.py: | ||
+ | <pre> | ||
+ | polls/ | ||
+ | __init__.py | ||
+ | admin.py | ||
+ | apps.py | ||
+ | migrations/ | ||
+ | __init__.py | ||
+ | models.py | ||
+ | tests.py | ||
+ | urls.py | ||
+ | views.py | ||
+ | </pre> | ||
+ | 编辑 urls.py,绑定 view: | ||
+ | <pre class="prettyprint"> | ||
+ | from django.conf.urls import url | ||
+ | |||
+ | from . import views | ||
+ | |||
+ | urlpatterns = [ | ||
+ | url(r'^$', views.index, name='index'), | ||
+ | ] | ||
+ | </pre> | ||
+ | 最后再编辑 mysite/urls.py,进行 url绑定: | ||
+ | <pre class="prettyprint"> | ||
+ | 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), | ||
+ | ] | ||
+ | </pre> | ||
+ | |||
+ | 再运行 | ||
+ | python manage.py runserver | ||
+ | |||
+ | 访问 http://localhost:8000/polls/ | ||
+ | |||
+ | 是不是看到刚编写的 view内容了?:) | ||
+ | |||
+ | Hello, world. You're at the polls index. |
2017年8月23日 (三) 16:03的最后版本
目录 |
安装
官网:https://www.djangoproject.com
Django当前最新版本是 1.11.4,1.11系列也是 Django支持 python2.7的最后一个系列版本,安装也很简单:
进入 python的安装目录,如默认的 C:\\Python27,再进入 Scripts目录,这里应该可以找到 pip.exe,cmd运行:
C:\Python27\Scripts>pip install Django==1.11.4
确认安装是否成功:
C:\Python27\Scripts>python Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> print django.get_version() 1.11.4
创建第一个project
1、进入你计算放置这个 project的目录,cmd 运行:
django-admin startproject mysite
如果提示找不到 django-admin,可把 C:\\Python27\Scripts加入系统环境路径中。
正常的话,会生成以下目录结构:
mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py
2、运行
python manage.py runserver
通过 http://127.0.0.1:8000/ 应该难看到欢迎界面了 :)
不过上面这仅是开发模式使用,要正式使用,需要在这个 project上创建 App.
创建app
进入 manage.py所在目录,运行:
python manage.py startapp polls
将生成以下目录结构:
polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
编写view
打开 polls/views.py,编辑进以下内容:
from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.")
配置
再创建一个url配置文件 urls.py:
polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py urls.py views.py
编辑 urls.py,绑定 view:
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), ]
最后再编辑 mysite/urls.py,进行 url绑定:
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), ]
再运行
python manage.py runserver
访问 http://localhost:8000/polls/
是不是看到刚编写的 view内容了?:)
Hello, world. You're at the polls index.