request
1. url传递参数
1)参数没有命名, 如:
users/views
def weather(request, city, year): print(city) print(year) return HttpResponse(‘OK‘)
users/urls
from django.conf.urls import url from . import views urlpatterns = [ # url(路径, 视图) url(r‘^weather/([a-z]+)/(\d{4})/$‘, views.weather, name=‘weather‘), ]
2) 参数命名, 如
from django.conf.urls import url from . import views urlpatterns = [ # url(路径, 视图) # url(r‘^weather/([a-z]+)/(\d{4})/$‘, views.weather, name=‘weather‘), url(r‘^weather/(?P<city>[a-z]+)/(?P<year>\d{4})/$‘, views.weather), ]
(?P<city> 就是给参数命名为city
原文地址:https://www.cnblogs.com/py-web/p/10900929.html
时间: 2024-10-17 16:38:53