django 后端反向生成url

 

导入模块

from django.core.urlresolvers import reverse

reverse()

reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
#

django视图

from news import views

url(r'^archive/$', views.archive, name='news-archive')

生成url

# using the named URL
reverse('news-archive')#直接写url

# passing a callable object
# (This is discouraged because you can't reverse namespaced views this way.)
from news import views
reverse(views.archive)
return redirect(reverse('login'))
#跳转路由里也能用

column_data = "<a href='%s' target='_blank'>%s</a>" % (reverse('students') + str(row_obj.id), eval('row_obj.' + '.'.join(column_list)))
#在tag里用

传参

from django.urls import reverse

def myview(request):
    return HttpResponseRedirect(reverse('arch-summary', args=[1945]))
#形参

>>> reverse('admin:app_list', kwargs={'app_label': 'auth'})
'/admin/auth/'
#关键字参数

 

posted @ 2017-10-22 19:24  陨落&新生  阅读(849)  评论(0编辑  收藏  举报