49๐
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__
In case one needs the class of the view since a class based view is being used one can access the view_class
of the returned function:
view_class = myfunc.view_class
7๐
From Django 2.0 onward django.core.urlresolvers
module has been moved to django.urls
.
You will need to do this:
from django.urls import resolve
myfunc, myargs, mykwargs = resolve("/hello_world/")
mymodule = myfunc.__module__
- [Django]-Django: Purpose of django.utils.functional.SimpleLazyObject?
- [Django]-How to combine django "prefetch_related" and "values" methods?
- [Django]-Django URLS, how to map root to app?
5๐
Since Django 1.3 (March 2011) the resolve
function in the urlresolvers
module returns a ResolverMatch
object. Which provides access to all attributes of the resolved URL match, including the view callable path.
>>> from django.core.urlresolvers import resolve
>>> match = resolve('/')
>>> match.func
<function apps.core.views.HomeView>
>>> match._func_path
'apps.core.views.HomeView'
- [Django]-Django-admin: Add extra row with totals
- [Django]-"Too many SQL variables" error in django with sqlite3
- [Django]-Compound/Composite primary/unique key with Django
1๐
1. Generate a text file with all URLs with corresponding view functions
./manage.py show_urls --format pretty-json --settings=<path-to-settings> > urls.txt
example
./manage.py show_urls --format pretty-json --settings=settings2.testing > urls.txt
2. Look for your URL in the output file urls.txt
{
"url": "/v3/affiliate/commission/",
"module": "api.views.affiliate.CommissionView",
"name": "affiliate-commission",
},
- [Django]-Convert seconds to hh:mm:ss in Python
- [Django]-Django most efficient way to count same field values in a query
- [Django]-Access Django models with scrapy: defining path to Django project
1๐
All the others focus on just the module or string representation of the view. However, if you want to directly access the view object for some reason, this could be handy
resolve('the_path/').func.cls
This gives the view object itself, this works on class based view, I havenโt tested it on a function based view though.
- [Django]-Django models โ how to filter number of ForeignKey objects
- [Django]-Celery: WorkerLostError: Worker exited prematurely: signal 9 (SIGKILL)
- [Django]-Django release 1.5: 'url' requires a non-empty first argument. The syntax changed in Django 1.5
0๐
Based on KillianDSโs answer, hereโs my solution:
from django.core.urlresolvers import resolve
def response(request, template=None, vars={}):
if template is None:
view_func = resolve(request.META['REQUEST_URI'])[0]
app_label = view_func.__module__.rsplit('.', 1)[1]
view_name = view_func.__name__
template = '%s.html' % os.path.join(app_label, view_name)
return render_to_response(template, vars, context_instance=RequestContext(request))
Now you can just call return response(request)
at the end of your view funcs and it will automatically load up app/view.html
as the template and pass in the request context.
- [Django]-Select between two dates with Django
- [Django]-How do I pass template context information when using HttpResponseRedirect in Django?
- [Django]-Which Stack-Overflow style Markdown (WMD) JavaScript editor should we use?