Is there any way to get a list of objects of one class without creating a dedicated view just using maybe TemplateView.as_view() and a template?
            Asked
            
        
        
            Active
            
        
            Viewed 219 times
        
    0
            
            
         
    
    
        frlan
        
- 6,950
- 3
- 31
- 72
- 
                    I'm not sure I know what you are asking here. If you are trying to get a dictionary of attributes for the specific instance where does your template come in? Do you mean you would like to list them? i.e. ``instance.__dict__`` When you say ``objects`` do you mean a list of all instantiated objects from your class? i.e. http://stackoverflow.com/questions/1535327/python-how-to-print-a-class-or-objects-of-class-using-print – Glyn Jackson Oct 12 '14 at 16:45
2 Answers
1
            
            
        In fact, you need something, that will be returning a response object. That is what view actually is.
If you don't want to declare view as function or class, you can use lambda-functions. Here is example of working urls.py:
from django.conf.urls import url
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
urlpatterns = [
        url(r'^test/$', lambda request: render_to_response('testapp/test.html', {'users': User.objects.filter()})),
]
I created anonymous function and returned response with objects I need and specified path to template.
 
    
    
        coldmind
        
- 5,167
- 2
- 22
- 22
1
            
            
        Sure. You can use assignment tag(write this in template tag library). Assignment tag docs
@register.assignment_tag
def my_tag():
    return Product.objects.all()
In template(TemplateView - no problem)
{% my_tag as my_tag %}
{% for item in my_tag %}
    {{ item.name }}
{% endfor %}
 
    
    
        nnmware
        
- 930
- 5
- 15