Using Django to develop websites, you can design very beautiful url rules. If the url matching rules (including regular expressions) are better organized, the view structure will be clearer and easier to maintain. But there may be some misunderstandings. Let’s summarize them below. hope its good for U.S..
Question:
The video I studied was probably recorded in 2015. The Django version used in it is relatively old. For the regular expression URL, url is used ("url(r' ^admin/', admin.site.urls),") method. When I was practicing myself, I downloaded the latest version, and the regular expression URL used the exact path ("path('admin/', admin.site.urls),") method. At the beginning, when a pair was matched, they were successful, so I didn't pay much attention to this detail.
Until later when multiple regular expressions match (path('detail-(\d+).html', views.detail),), the error "page not found" will always be reported. I checked it several times. It's obviously exactly the same. Why can't it work? Actually, the color feels a little off, but I don’t know why. Later, after checking the relevant version documents, I finally found a solution!
The relevant code is attached below:
1. This is the most important py file, and the others are almost the same.
2. Other related codes:
views.py
def detail(request,nid): # print(nid) # return HttpResponse(nid) detail_info = USER_DICT[nid] return render(request,'detail.html',{'detail_info':detail_info})
index.html
detail.py
In addition, when the URL needs to pass multiple data, it must be passed strictly in order, and the function definition must have a corresponding number of parameters. Of course, you can use universal parameters to receive any number of participants
General The regular expression used in this case is: url(r'^detail-(?P
and the corresponding The detail function is like this: def detail(request, *args, **kwargs):pass
Related recommendations:
Detailed explanation of website deployment based on Django framework
The above is the detailed content of Misunderstandings about regular expression URLs in Django framework. For more information, please follow other related articles on the PHP Chinese website!