Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django学习记录(二):URL解析 #38

Open
PyxYuYu opened this issue Sep 14, 2016 · 0 comments
Open

Django学习记录(二):URL解析 #38

PyxYuYu opened this issue Sep 14, 2016 · 0 comments
Labels

Comments

@PyxYuYu
Copy link
Owner

PyxYuYu commented Sep 14, 2016

After all, tomorrow is another day.

0x01 Django

  • 正常解析 url
    • 利用 URLconf 来进行解析
from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]
  • 逆向解析 url

    • HTML 模板 templates,使用 url 标记进行解析
      • 不带参数
        • {% url 'name' %}
    • 带参数:参数可以是变量名
      • {% url 'name' 参数 %}
    • 只要 name 不变,正则发生变化,网址也会动态的发生变化
    <!-- add 就是 name 属性 -->
    <a href="{% url 'add' 1 2 %}">link</a>
    url(r'^new_add/(\d+)/(\d+)/$', views.add, name='add')
    # 只要url中的正则发生变化,那么网址也会改变,而不需要重新写一个new_add的name属性
    • django.core.urlresolvers.reverse() 方法解析
    from django.core.urlresolvers import reverse
    from django.http import HttpResponseRedirect
    def old_add(request, a, b):
    
          return HttpResponseRedirect(reverse('add', args=(a, b)))
    • get_absolute_url() 方法解析
0x02 正则表达式

  • ^$ 表示空行,是字符串的开始同时也是字符串的结尾,中间没有任何字符
    • 这个命令在 Linux 中也代表了空行
@PyxYuYu PyxYuYu added the Django label Sep 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant