Django URL管理

URL管理

1、创建好Project后在全局配置文件中有一个urls.py这个模块,该模块主要管理本项目的全局url配置

2、每个APP也应该创建一个urls.py模块来管理自己APP下的url集(可选)

 

全局urls.py配置

主要注意的是

1、需要import  include模块

2、在urlpatterns中添加app下的urls模块, namespace 参数可以防止不同app下的 url name

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog/', include('blog.urls', namespace="blog")),
]

 

APP下的urls.py

1、import django中url模块

2、import app中的views模块

3、需要注意的是url是以正则表达式来配置管理,访问web页面的url = 全局url+APP url

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name="index"),
]

 

URL.py中的正则表达式

这里需要注意的是:

1、如果正则里面有()括号表示为分组,会自动将()括号里面的内容传到views.article_page视图中。

2、如果正则格式为:(?P<>) 表示为命名分组,在View视图里面或者template调用的时候,可以直接使用命名去调用

 

在url.py中我们可以使用正则表达式来匹配url

urlpatterns = [
    url(r'^$', views.index),
    url(r'^article/(?P<article_id>[0-9]+)$', views.article_page),
]

其中(?P<article_id>[0-9]+)不是很好理解,先来看看re官方的解释:

“(?P<name>...) 子串匹配到的内容将可以用命名的name来提取url中的值。组的name必须是有效的python标识符,而且在本表达式内不重名。”

 

Templates href格式

href="{% url ‘app_name:url_name’ param %}" 

app_name和url_name中需要在全局的urls.py和app下的urls.py中配置好 

两种方式:

第一种

   

第二种

#第一步在app-url.py的url中添加一个name参数:name='app-views-func'

urlpatterns = [
    url(r'^$', views.index),
    url(r'^article/(?P<article_id>[0-9]+)$', views.article_page, name='blog-views-article_page'),
]

#第二步在Template中编写href
href="{% url 'blog-views-article_page' article.id %}"

Django官方url用法

 

正则表达是的命名分组(?P<>)

1、简单分组

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/([0-9]{4})/$', views.year_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]

2、有名分组(有名分组就可以在views.py中通过 关键字参数 year, month,day)

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
    url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),
]

 

应用案例:

urls.py 相关代码:

 url(r'^bookdetail/(?P<book_id>\d+)/$', BookDetailView.as_view(), name='bookdetail'),

 

Views.py代码: 从url中接收book_id

class BookDetailView(View):
    """
    图书详情
    """
    def get(self, request, book_id):
        book_detail = Book.objects.get(id=int(book_id))
return render(request, "book_detail.html", {"book_detail": book_detail})

 

HTML代码:

<table class="table table-hover">
    <thead>
        <tr>
            <th>No.</th>
            <th>书名</th>
            <th>作者</th>
            <th>出版日期</th>
            <th>定价</th>
        </tr>
    </thead>
    <tbody>
    {% for book in all_books.object_list %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td><a href="{% url "bookdetail" book.id %}">{{ book.name }}</a></td>
            <td>{{ book.author }}</td>
            <td>{{ book.publish_date|date:"Y-m-d" }}</td>
            <td>{{ book.price|floatformat:2 }}</td>
        </tr>
    {% empty %}
        <tr>
            <td>暂无图书</td>
        </tr>
    {% endfor %}
    </tbody>
</table>

 

posted @ 2017-02-22 23:33  Vincen_shen  阅读(7882)  评论(0编辑  收藏  举报