美文网首页
django学习笔记(四)

django学习笔记(四)

作者: beed0c3eb989 | 来源:发表于2017-09-01 21:33 被阅读0次

    url地址

    learn/urls.py

    from django.conf.urls import include, url
    from django.contrib import admin
    from blog.views import index
    
    urlpatterns = [
        url(r'^admin/', include(admin.site.urls)),
        url(r'^blog/index/$',index)
    ]
    
    from django.conf.urls import include, url
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^admin/', include(admin.site.urls)),
        url(r'^blog/index/$','blog.views.index')
    ]
    

    url传参

    ①关键字参数

    learn/urls.py

    urlpatterns = [
        url(r'^admin/', include(admin.site.urls)),
        url(r'^blog/index/(?P<id>\d{2})/$','blog.views.index')
    ]
    

    blog/views.py

    def index(request,id):
        user={'name':'Tom','age':34,'sex':'male'}
        #user=Person('tom',23,'male')
        book_list = ['Python','java','php','web']
        return render_to_response('test1.html',{'title':'Hello','user':user,'book_list':book_list,'id':id})
    
    ②未命名参数

    learn/urls.py

    urlpatterns = [
        url(r'^admin/', include(admin.site.urls)),
        url(r'^blog/index/(\d{2})/$','blog.views.index')
    ]
    

    blog/views.py

    def index(request,p):
        user={'name':'Tom','age':34,'sex':'male'}
        #user=Person('tom',23,'male')
        book_list = ['Python','java','php','web']
        return render_to_response('test1.html',{'title':'Hello','user':user,'book_list':book_list,'id':p})
    

    相关文章

      网友评论

          本文标题:django学习笔记(四)

          本文链接:https://www.haomeiwen.com/subject/yfokjxtx.html