0,访问原理
1,name字段的好处:
如果有需要改动链接格式:url.py,能实现保持模版里的link url不变:home.html
url(r'^add2/$',views.add,name='add')
zqxt_tmpl/
├── db.sqlite3
├── learn
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models.py
│ ├── templates
│ │ └── home.html
│ ├── tests.py
│ └── views.py
├── manage.py
└── zqxt_tmpl
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
url.py
from django.conf.urls import url
from django.contrib import admin
from learn import views
urlpatterns = [
url(r'^$',views.home,name='home'),
url(r'^add2/$',views.add,name='add'),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request,'home.html')
def add(request):
a=request.GET['a']
b=request.GET['b']
c=int(a)+int(b)
return HttpResponse(str(c))
home.html
<h1>欢迎lanny</h1>
<h1>
<a href="{% url 'add' %}">link</a>
</h1>
2,测试:
修改url.py
url(r'^add/$',views.add,name='add'), 为:
url(r'^add2/$',views.add,name='add'),
但是并未改动home.html里的 add
<a href="{% url 'add' %}">link</a>
可见django对url有优雅的支持.
转载于:https://blog.51cto.com/lannyma/1750156