标签:
Django 视图与网址进阶
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘web_app‘, #
python manage.py startapp web_app # web_app 是一个app的名称]
# -*- coding: utf-8 -*-
from django.http import HttpResponse
def index(request):
return HttpResponse(u"欢迎登陆 ZJ 自动化运维平台")
from django.conf.urls import url
from django.contrib import admin
from web_app import views as web_views
admin.autodiscover()
urlpatterns = [
url(r‘^$‘,web_views.index),
url(r‘^admin/‘, admin.site.urls),
]
django-admin.py startproject HelloDjango
cd HelloDjango
python manage.py startapp HelloDjango
# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render
def add(request):
a = request.GET[‘a‘]
b = request.GET[‘b‘]
c = int(a)+int(b)
return HttpResponse(str(c))
from django.conf.urls import url
from django.contrib import admin
from web_app import views as web_views
urlpatterns = [
url(r‘^add/‘, web_views.add, name=‘add‘),
url(r‘^$‘,web_views.index),
url(r‘^admin/‘, admin.site.urls),
]
- # -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render
def add(request):
a = request.GET[‘a‘]
b = request.GET[‘b‘]
c = int(a)+int(b)
return HttpResponse(str(c))
def add2(request, a, b): #接受参数。
c = int(a) + int(b)
return HttpResponse(str(c))
from django.conf.urls import url from django.contrib import admin from web_app import views as web_views urlpatterns = [ url(r‘^add2/(\d+)/(\d+)/$‘, web_views.add2, name=‘add2‘), #新添加一行 url(r‘^add/‘, web_views.add, name=‘add‘), url(r‘^$‘,web_views.index), url(r‘^admin/‘, admin.site.urls), ]
Django 1.7.x 及以下:
url(r‘^add/(\d+)/(\d+)/$‘, ‘calc.views.add2‘, name=‘add2‘),
Django 1.8.x 及以上:
url(r‘^add2/(\d+)/(\d+)/$‘, web_app.views.add2, name=‘add2‘),
from django.conf.urls import url
from django.contrib import admin
from calc import views as web_views
urlpatterns = [
url(r‘^add/‘, web_views.add, name=‘add‘),
url(r‘^add2/(\d+)/(\d+)/$‘, web_views.add2, name=‘add2‘),
url(r‘^admin/‘, admin.site.urls),
]
标签:
原文地址:http://www.cnblogs.com/zhangju/p/5836535.html