Misunderstandings about regular expression URLs in Django framework

小云云
Release: 2023-03-19 17:50:01
Original
1772 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

    Title 
    {% for k,v in user_dict.items %}
  • {{ v.name }}
  • {% endfor %}
Copy after login

detail.py

    Title 

详细信息

用户名:{{ detail_info.name }}
邮箱:{{ detail_info.email }}
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!