preface
For example, open the time classification tab of the blog Garden
https://www.cnblogs.com/canglongdao/archive/2020/07.html , the time parameters are dynamic. If I want to obtain the time parameters 2020 and 07, it involves the acquisition of url parameters.
Get url parameters
First use path to match a url address, similar to: Archive / 2020 / 07 HTML, so take the two parameter names year and month
Parameters are in the format of < name >
#helloworld/helloworld/urls.py from django.conf.urls import url from django.urls import re_path,path from xjyn import views urlpatterns=[ path("archive/<year>/<month>.html",views.hh), ]
xjyn.py/views.py view function content
from django.shortcuts import render from django.http import HttpResponse,Http404 # Create your views here. def hh(request,year="2020",month="07"): return HttpResponse("Gets the time stamp of the current page{}year/{}month".format(year,month))
After starting the service, enter the address in the browser: http://127.0.0.1:8000/archive/2020/7.html
Regular matching url
Although the above case can obtain parameters from the url, it will encounter a problem. Various data can be input for year and month, such as archive / 2020 / 101 HTML, obviously unreasonable.
If you want the year parameter to be only 4 numbers and the month parameter to be only 2 numbers, what should you do? This requires regular matching.
- ? p parameter year
- [0-9] number matching 0-9
- {4} Match 4 numbers
- {1,2} matches 1-2 numbers
- r is the raw prototype and is not escaped
- ^Match start
- $match end
#helloworld/helloworld/urls.py from django.conf.urls import url from django.urls import re_path,path from xjyn import views urlpatterns=[ path("archive/<year>/<month>.html",views.hh), url(r'^archive1/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2}).html$',views.hh1), ]
xjyn.py/views.py view function content
from django.shortcuts import render from django.http import HttpResponse,Http404 # Create your views here. def hh(request,year="2020",month="07"): return HttpResponse("Gets the time stamp of the current page{}year/{}month".format(year,month)) def hh1(request,year="2020",month="07"): return HttpResponse("Gets the time stamp of the current page{}year/{}month".format(year,month))
After starting the service, enter the address in the browser: http://127.0.0.1:8000/archive1/2020/11.html,http://127.0.0.1:8000/archive1/2020/1.html You can enter 2 digits or 1 digit of the month.
Enter 3 digits in the month and report an error;
urls. Role of name defined in PY
If there is an a.html page and a b.html page, the previous two pages are independent and don't want to do it. If you need to click a button from page a to jump to b.html, how can you realize it?
xjyn/templates/a.html write the following contents;
<base href=" http://127.0.0.1:8000/ ",target="_ Blank "> this sentence must be written. Otherwise, when you jump to the B page, the address is abnormal (/ a/b);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Wuhan-meeting</title> <base href="http://127.0.0.1:8000/",target="_blank"> </head> <body> <p> Welcome to study django! <br> <a href="b/">Point here b page</a> </p> </body> </html>
xjyn/templates/b.html is written as follows
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>b page</title> </head> <body> <p> <h4>This is my blog, you can baidu search: starry sky 6</h4> <p>The luckier you are, the harder you work</p> </p> </body> </html>
xjyn/views.py file; Function names can be named at will, as long as URLs Py, write the corresponding function name;
from django.shortcuts import render from django.http import HttpResponse,Http404 # Create your views here. def he(request): return render(request,"a.html") def hehe(request): return render(request,"b.html")
xjyn/urls.py file content; The path in the url can be named at will. When accessing, you can access according to the path;
#helloworld/helloworld/urls.py from django.conf.urls import url from django.urls import re_path,path from xjyn import views urlpatterns=[ url('^b/$',views.hehe), url('^a/$',views.he), ]
If the url address is written on the page:
< a href = "b /" > Click here to page b < / a >, which will have a disadvantage. When multiple pages use this address, if the subsequent address changes, it will be difficult to maintain.
For the convenience of maintaining the url address, you can give it a unique name, that is, the name parameter, and then add a name in the url configuration.
name can be named at will, and it can be consistent with it in the web page;
#helloworld/helloworld/urls.py from django.conf.urls import url from django.urls import re_path,path from xjyn import views urlpatterns=[ url('^okk/$', views.hehe,name="b_pages"), url('^a/$', views.he,name="a_pages"), ]
Change the address of xjyn/templates/a.html jump to the following:
<a href="{% url 'b_pages' %}">b page</a>