首页 > 代码库 > django -- url 的 name 属性
django -- url 的 name 属性
在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="http://www.mamicode.com/提交" />
</form>
</body>
</html>
由上述代码可以看到url的name使用方法。
django -- url 的 name 属性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。