首页 技术 正文
技术 2022年11月22日
0 收藏 766 点赞 3,670 浏览 3921 个字

django的环境安装非常简单,只需用pip安装一个django库就可以了,编辑器选择pycharm

pip install django==2.1.2

查看版本号:pip show django

C:\Users\dell>pip show django
Name: Django
Version: 2.1.2
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: e:\python36\lib\site-packages
Requires: pytz
Required-by:

安装完之后在cmd检查下是否能用

C:\Users\dell>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
2.1.2
>>>

创建项目

先建一个工程,比如我的项目代码想放到E:\web_djo目录下,然后新建一个Django project( 即一个 Django 项目实例需要的设置项集合,包括数据库配置、Django 配置和应用程序配置。)
打开命令行,cd 到一个你想放置你代码的目录,然后运行以下命令:

django-admin startproject helloworld

执行完之后打开pycharm就可以看到web_djo工程目录下多了以下层级文件

─helloworld
│ manage.py

└─helloworld
settings.py
urls.py
wsgi.py
__init__.py

这些目录和文件的用处是:

  • 最外层的:helloworld: 项目的容器,可以随便命名。
  • manage.py: 一个让你用各种方式管理 Django 项目的命令行工具。
  • helloworld/__init__.py:一个空文件,告诉 Python 这个目录应该被认为是一个 Python 包。
  • helloworld/settings.py:Django 项目的配置文件。
  • helloworld/urls.py:Django 项目的 URL 声明,就像你网站的“目录”。
  • helloworld/wsgi.py:作为你的项目的运行在 WSGI 兼容的Web服务器上的入口。

django-admin

django-admin.exe是一个可执行文件,安装django时候会默认安装到python3\Scripts目录下,相关指令用-h查看

E:\python36\Scripts>django-admin -hType 'django-admin help <subcommand>' for help on a specific subcommand.Available subcommands:[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

启动服务

接下来启动django服务,使用helloworld下的manage.py,先cd到web_djo/helloworld目录下,到在命令行输入以下指令:

python manage.py runserver

E:\web_djo\helloworld>python manage.py runserver
Performing system checks...System check identified no issues (0 silenced).You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 23, 2018 - 21:09:39
Django version 2.1.2, using settings 'helloworld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

启动完只看看到这句:Starting development server at http://127.0.0.1:8000/,复制地址在浏览器打开

django服务默认在8000端口启动,如果想换个端口,可以输入以下指令

python manage.py runserver 8080

如果一个局域网另外一台电脑也需要能访问,可以监听所有ip:python manage.py runserver 0.0.0.0:8000,访问的时候用电脑ip代替127.0.0.1

用于开发的服务器在需要的情况下会对每一次的访问请求重新载入一遍 Python 代码。所以你不需要为了让修改的代码生效而频繁的重新启动服务器。然而,一些动作,比如添加新文件,将不会触发自动重新加载,这时你得自己手动重启服务器。

视图和URL配置

在先前创建的helloworld/helloworld目录新建一个 view.py 文件,并输入代码

# helloworld/helloworld/view.py
from django.http import HttpResponsedef index(request):
return HttpResponse("Hello world ! django ~~")

绑定URL与视图函数。打开 urls.py 文件,删除原来代码,将以下代码复制粘贴到 urls.py 文件中

# helloworld/helloworld/urls.py
from django.conf.urls import url
from . import viewurlpatterns = [
url(r'^$', view.index),
]

url函数

url() 可以接收四个参数,分别是两个必选参数:regex、view 和两个可选参数:kwargs、name.

def url(regex, view, kwargs=None, name=None):
return re_path(regex, view, kwargs, name)
  • regex: 正则表达式,与之匹配的 URL 会执行对应的第二个参数 view。

  • view: 用于执行与正则表达式匹配的 URL 请求。

  • kwargs: 视图使用的字典类型的参数。

  • name: 用来反向获取 URL。

多个url设置

urlpatterns里面url(r’^$’, view.index)这项是打开首页http://127.0.0.1:8000,平常网站会有多个页面,如果想加个页面地址如:http://127.0.0.1:8000/yoyo打开另外一个页面.

view.py加个函数

from django.http import HttpResponsedef index(request):
return HttpResponse("Hello world ! django ~~ ")def yoyo(request):
return HttpResponse("yoyo! django ~~ ")

urls.py加个配置

from django.conf.urls import url
from . import viewurlpatterns = [
url('^$', view.index),
url('^yoyo$', view.yoyo),
]

这样在浏览器上输入地址:http://127.0.0.1:8000/,打开页面出现:Hello world ! django ~~
在浏览器输入地址:http://127.0.0.1:8000/yoyo, 打开页面出现:yoyo! django ~~

关于regex正则表达式用法可以参考菜鸟教程http://www.runoob.com/regexp/regexp-tutorial.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289