在html的form中使用给url定义的name值,可以在修改url时不用在修改form的src。
urls.py
from django.conf.urls import url from mytest import views urlpatterns = [ # url(r‘^admin/‘, admin.site.urls), url(r‘^index/‘, views.index, name=‘mysite‘), views.Index.as_view()), ]
views.py
from django.http import HttpResponse from django.shortcuts import render def 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
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <form action="{% url ‘mysite‘ %}" method="post"> <input type="text" name="A" /> <input type="submit" name="b" value="提交" /> </form> </body> </html>
由上述代码可以看到url的name使用方法。
时间: 2024-10-25 01:03:09