Django based URL routing

Read, study and learn about everything important in your life
Everything is important, learning everywhere is block

There is a urls.py file in Django, which is used to manage django urls and routes. We can create or modify routes in the urls.py file to achieve different view functions for different urls.

urls.py

First look at the urls.py long sample?

1
2
3
4
5
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),

By default, django has only one route, admin. When we open manage.py, we can only access the 127.0.0.1:8000/admin/ directory, and the rest cannot be accessed.

Creating a route

As you can see, the creation of a route conforms to the rules of a regular expression, ^ means start, and $ means end;
In addition, the matching of the routes is from top to bottom, that is, the matching is started from the first route, and if it is satisfied, it does not match, if it does not match, it continues to go down. Therefore, in order to avoid accessing the url that does not exist, resulting in an error, you can add a route matching any url, you can jump to the 404 page (defined by yourself), or you can jump to the home page.

1
2
3
4
5
6
7
8
from django.conf.urls import url
from django.contrib import admin
From webapp import views #import the app's views file
urlpatterns = [
url(r'^admin/', admin.site.urls),
Url(r'^.*$',views.index,name="index"), #define the universal route, note that this route must be placed last.

URL pseudo-static transformation

Generally when we want to pass the argument, the url is similar: 127.0.0.1/app/?a=1&b=2
At this point the configuration of urls.py is like this:

1
2
3
4
5
from django.conf.urls import url
From webapp import views #import the app's views file
urlpatterns = [
url(r'^app/$', views.app , name="app"),

At this point the view.py code looks like this:

1
2
3
4
5
6
def app(request):
a=request.GET.get("a")
b=request.GET.get("b")
return HttpResponse(a+b)

However, if we want to change the URL to: 127.0.0.1/app/1/2/?

Modify urls.py:

1
2
3
4
5
from django.conf.urls import url
From webapp import views #import the app's views file
urlpatterns = [
url(r'^app/(\d+)/(\d+)/$', views.app , name="app"),

Modify view.py:

1
2
3
4
def app(request,a,b):
return HttpResponse(str(int(a)+int(b)))

Url name

We see that there is a name field in the routing configuration in urls.py, but it is optional, but it is recommended to write it.

1
2
urlpatterns = [
url(r'^app/$', views.app , name="app"),

Explanation: name is equivalent to giving this route a name. The advantage is that the regularity of the route may change frequently. The url in the html also needs to change, because it needs to correspond to the route in urls. But if you give the route a name, you can use the name of the route in the html, so that when the regularity of the route changes, but as long as the name does not change, html does not need to be changed.

This can be used in an HTML page:

1
2
3
4
5
6
{% url 'name' %} without parameters
{% url 'name' parameter %} with parameters: parameter can be variable name
<a href="{% url 'app' 4 5 %}">link</a>
<a href="{% url 'app' %}">link</a>
<a href="{% url 'app' %}?a=1&b=2">link</a>

本文标题:Django based URL routing

文章作者:nmask

发布时间:2017年09月15日 - 15:09

最后更新:2019年07月11日 - 15:07

原始链接:https://thief.one/2017/09/15/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: