从请求uri获取视图功能? [英] Get view function from request uri?

查看:39
本文介绍了从请求uri获取视图功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个像/home/这样的uri,我想找到它对应的视图函数,最好是像 app.views.home 这样的形式,或者只是< app_label>.< view_func> .有功能可以给我这个吗?

Given a uri like /home/ I want to find the view function that this corresponds to, preferably in a form like app.views.home or just <app_label>.<view_func>. Is there a function that will give me this?

推荐答案

您可以使用解析方法来获取该功能.您可以使用返回函数的 __ module __ 属性来获取应用标签.这将返回类似 project.app.views 的字符串.像这样:

You can use the resolve method provided by django to get the function. You can use the __module__ attribute of the function returned to get the app label. This will return a string like project.app.views. So something like this:

from django.urls import resolve

myfunc, myargs, mykwargs = resolve("/hello_world/")
mymodule = myfunc.__module__

这篇关于从请求uri获取视图功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆