導入模塊django
from django.core.urlresolvers import reverse
reverse()
reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None) #
django視圖app
from news import views url(r'^archive/$', views.archive, name='news-archive')
生成urlthis
# 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裏用
傳參url
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/' #關鍵字參數