在html的form中使用给url定义的name值,可以在修改url时不用在修改form的src。
urls.py
from django.conf.urls import urlfrom mytest import viewsurlpatterns = [ # url(r'^admin/', admin.site.urls), url(r'^index/', views.index, name='mysite'),views.Index.as_view()),]
views.py
from django.http import HttpResponsefrom django.shortcuts import renderdef index(req): if req.method == 'POST': print('method is :' + req.method) elif req.method == 'GET': print('method is :' + req.method) return render(req, 'index.html')
index.html
index
由上述代码可以看到url的name使用方法。