开发者

Django Generic View - Access to request

I am using django generic views, how do I get access to the request in my template.

URLs:

file_objects = {
    'queryset' : File.objects.filter(is_good=Tru开发者_JS百科e),
}
urlpatterns = patterns('',
    (r'^files/', 'django.views.generic.list_detail.object_list', dict(file_objects, template_name='files.html')),
)


After some more searching, while waiting on people to reply to this. I found:

You need to add this to your settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
)

This means that by default the request will be passed to all templates!


None of the answers given solved my issue, so for those others who stumbled upon this wanting access to the request object within a generic view template you can do something like this in your urls.py:

from django.views.generic import ListView

class ReqListView(ListView):
    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        c = super(ReqListView, self).get_context_data(**kwargs)
        # add the request to the context
        c.update({ 'request': self.request })
        return c

url(r'^yourpage/$',
    ReqListView.as_view(
        # your options
    )
)

Cheers!


Try using the get_queryset method.

def get_queryset(self):
    return Post.objects.filter(author=self.request.user)

see link (hope it helps):- See Greg Aker's page...


What works for me was to add:

TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
                           "django.core.context_processors.request",
                           )

To the the settings.py not to the urls.py

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜