Django - urls.py 中的访问会话
我试图使用 ListView 来避免为应该非常简单的页面创建视图。 基本上我想列出一组与当前用户相关的对象,但是我不确定如何从 urls.py 中访问会话值。
我所拥有的看起来像这样:
(r'^myrecords/$', ListView.as_view(
queryset=Record.objects.filter(CURRENT LOGGED IN USER),
context_object_name='record_list',
template_name='records.html')),
我需要做什么?
还有什么方法可以应用 login_required 装饰器吗?
任何建议将不胜感激。
谢谢。
I'm trying to use a ListView to avoid creating a view for what should be quite a simple page.
Basically I want to list a set of objects related to the current user, however I'm not sure how to access the session values from within the urls.py.
What I have looks something like this:
(r'^myrecords/
What do I need to do?
Also is there any way to apply the login_required decorator to this?
Any advice would be greatly appreciated.
Thanks.
, ListView.as_view(
queryset=Record.objects.filter(CURRENT LOGGED IN USER),
context_object_name='record_list',
template_name='records.html')),
What do I need to do?
Also is there any way to apply the login_required decorator to this?
Any advice would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅动态过滤上的文档。
您应该子类化
ListView
并重写get_queryset
方法。调用基于类的视图时,self
会填充当前请求 (self.request
) 以及从 URL 捕获的任何内容 (self.args
,self.kwargs
)。还有装饰基于类的文档视图。基本上,您可以在
urls.py
中装饰as_view
的结果:See the docs on dynamic filtering.
You should subclass
ListView
and override theget_queryset
method. When the class-based view is called,self
is populated with the current request (self.request
) as well as anything captured from the URL (self.args
,self.kwargs
).There's also documentation for decorating class-based views. Basically you can just decorate the result of
as_view
in yoururls.py
: