urls
-
搭建项目目录结构
-
初始化项目目录结构
可以假如项目配置如下:项目文件夹:appurl项目名:appcalc主机访问地址:www.app.com
搭建方法参考:
-
-
采用/add/?a=3&b=5(GET)
-
修改appcalc/views.py
把views.py做如下修改:from django.shortcuts import renderfrom django.http import HttpResponse def add(req): a = req.GET['a'] b = req.GET['b'] c = int(a)+int(b) return HttpResponse(str(c))
-
修改appurl/urls.py
添加url:from django.conf.urls import urlfrom django.contrib import adminfrom calc import views as calc_views urlpatterns = [ url(r'^add/$', calc_views.add, name='add'), # 新增 url(r'^admin/', admin.site.urls),]
主机访问
启动应用,命令行输入:
python manage.py runserver 0.0.0.0:8000
主机浏览器上输入:www.app.com:8000/add/?a=6&b=10
页面输出:16 -
-
使用/add/9/20这种形式
-
修改appcalc/views.py
添加新的方法add2,如下所示def add2(req, a, b): c = int(a) + int(b) return HttpResponse(str(c))
-
修改/appurl/urls.py
添加新的urlurl(r'^add/(\d+)/(\d+)/$, appcalc_views.add2, name="add2")
-