[Fixed]-Combining multiple django apps into one view, what am I doing wrong?

1👍

You cannot do this like that. Views are glorified functions.
They have a single entry point (dispatch()) that is invoked by Django internals, and invokes the correct method on itself, depending on the HTTP method (here, get()).
Casting the view to a string will just display its name, not call it.

If you want to aggregate the behaviors of multiple view, you have several options.

  • you could extract those behaviors in functions, and have your actual views call those functions.
  • you could make mixins, and mix them into the views.

For instance:

from django.views.generic.base import ContextMixin

class ResourceListMixin(ContextMixin):
    def get_resource_list(self):
        # we put it in a separate method to allow easy overriding
        return Submissions.objects.all()

    def get_context_data(self, **kwargs):
        kwargs['resources'] = self.get_resource_list()
        return super(ResourceListMixin, self).get_context_data(**kwargs)

Now you can mix that into any view, and it will automagically know about{{ resources }}. For instance:

from django.views.generic.base import TemplateView

class IndexView(ResourceListMixin, TemplateView):
    template_name = 'index.html'
    # nothing else here, we leverage the power of Django's
    # TemplateView - it will do everything by itself

Then the IndexView view will render the index.html template, and it will be able to do {% for resource in resources %}.

Leave a comment