Home > Backend Development > PHP Tutorial > Misunderstandings about regular expression URLs in Django framework

Misunderstandings about regular expression URLs in Django framework

小云云
Release: 2023-03-19 17:50:01
Original
1888 people have browsed it

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})
Copy after login

index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 <ul>
  {% for k,v in user_dict.items %}
   <li><a target="_blank" href="/detail-{{ k }}.html" rel="external nofollow" >{{ v.name }}</a></li>
  {% endfor %}
 </ul>
</body>
</html>
Copy after login

detail.py

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 <h1>详细信息</h1>
 <h6>用户名:{{ detail_info.name }}</h6>
 <h6>邮箱:{{ detail_info.email }}</h6>
</body>
</html>
Copy after login

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\d+)-(?P\d+)', views.detail),

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template