1. Open
urls.py
fromDjango.conf.urlsImportURL fromDjango.contribImportAdmin fromDjango_appImportViewsurlpatterns=[url (r'^admin/', admin.site.urls), url (r'^index/', views.index), url (r'^$', views.index), url (r'^login', views.login), url (r'^add/$', views.add,name='Add'), url (r'^add/(\d+)/(\d+)/$', VIEWS.ADD2, Name='ADD2'),]
URL (r ' ^add/$ ', views.add,name= ' Add ') Here's name= ' add ' for what?
Simply put, name can be used in templates, models, views ... Get the corresponding URL, equivalent to "give a name to the URL", as long as the name is not changed, the URL can also be obtained by name.
To get to the bottom of this question, we'll start with a first page view and URL
2. Modify the views.py file
from Import HttpResponse from Import def Index (Request): return'home.html' )
Render is a render template
When you use Render, Django automatically finds files in templates under each app listed in Installed_apps.
Tips, Debug=true, Django can also automatically find the static files under each app (Js,css, pictures and other resources), easy to develop
3. Create a new home.html in the app's templates
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title></Head><Body><ahref= "/ADD/4/5">Calculate 4+5</a></Body></HTML>
Modify urls.py
fromDjango.conf.urlsImportURL fromDjango.contribImportAdmin fromDjango_appImportViewsurlpatterns=[url (r'^admin/', admin.site.urls), url (r'^index/', views.index), url (r'^$', views.index,name='Home'), #修改部分 URL (r'^login', views.login), url (r'^add/$', views.add,name='Add'), url (r'^add/(\d+)/(\d+)/$', VIEWS.ADD2, Name='ADD2'),]
To run the development server, we visit http://127.0.0.1:8000/to see.
When we calculate the addition, we use/add/4/5/, and then the demand changes , such as change to/4_add_5/, but in the Web page, many places in the code are written dead/add/4/5/, such as the template may be written
<href= "/add/4/5/"> calculate 4+5</a>
If you write a "dead url", it will make the template (templates), the view (views.py, used to jump), the model ( models.py, can be used to get the address of the object corresponding to the use of this URL, all have to make corresponding changes, the cost of modification is very large, accidentally, some places did not change, it can not be used.
What's the answer?
Let's talk about how to get the corresponding URLs in Python code (which can be used in various places where the views.py,models.py need to be converted to URLs):
from django.urls import Reversereverse ( " add2 ", args= (4,5 3": " /add/4/5/" Span style= "COLOR: #800000" > " reverse ( " add2 ", args= (444,555 4]: " /add/444/555/ "
reverse receive the name in the URL as the first parameter , we can be in the code through reverse () to obtain the corresponding URL (this URL can be used to jump, also can be used to calculate the address of the relevant page), as long as the corresponding URL name does not change, You don't have to change the URL in your code.
In the Web template is the same, can be very convenient to use.
without parameters: {% url ' name '%} with parameter: parameter can be variable name {% URL ' name ' parameter%} for example: < href= "{% url ' add2 ' 4 5}">link</a>
The above code renders as the final page is
<href= "/add/4/5/">link</a>
This allows you to get to the corresponding URL by {% url ' add2 ' 4 5}/add/4/5/
When the urls.py changes, the premise is not to change the name (this parameter is not easy to change), access to the URL will be changed dynamically, for example, changed to:
URL (r'^new_add/(\d+)/(\d+)/$', VIEWS.ADD2, name='add2' ),
Note that the focus of add into the New_add, but the back of the Name= ' ADD2 ' did not change, at this time {% url ' add2 ' 4 5} will render the corresponding URL into/new_add/4/5/
The reverse function, which is used in places such as views.py or models.py, also gets a new URL based on the URL of name.
If you want to change the URL, modify the regular expression section in the urls.py (the first part of the URL parameter), and the name will not change anywhere else.
In addition, for example, the user favorite collection of the URL is old, how to let the previous/add/3/4/automatically jump to the new URL now?
to know that Django won't do this for you, this needs to write a jump method yourself :
The specific idea is to write a jump function in views.py:
from Import Httpresponseredirect from Import Reverse def Old_add2_redirect (Request, A, B): return Httpresponseredirect ( reverse ('add2', args= (A, B))
Add the following two records to the urls.py:
URL (r'^add/(\d+)/(\d+)/$', views.old_add2_redirect), url (r'^new _add/(\d+)/(\d+)/$', VIEWS.ADD2, name='add2'),
This way, if you have/add/4/5/ in your Favorites folder , you will automatically be redirected to the new/new_add/4/5/when you access it.
Django URL Name Detailed