Django 类型的重载视图
如何在 Django 中创建重载视图? 问题是我的观点可能只接受一个论点,也可能不接受。 如果有参数,它会将其传递给视图。
让我们展示一下我正在尝试执行的操作
views.py
def member_list(request,message):
memberList = Member.objects.all()
return render_response(request, 'user/member_list.html', {'memberList': memberList, 'message':message })
def member_list(request,message):
memberList = Member.objects.all()
return render_response(request, 'user/member_list.html', {'memberList': memberList, 'message':message })
urls.py
(r'^member/list/$', 'views.member_list'),
(r'^member/list/(?P<message>[-\w]+)/$', 'views.member_list'),
执行此操作的正确方法是什么?
谢谢
How is this possible to make overloaded views in Django ?
The problem is I have a view which may accept exactly one argument or none.
If there is a argument, it will pass it to view.
Let show what I am tryin' to do
views.py
def member_list(request,message):
memberList = Member.objects.all()
return render_response(request, 'user/member_list.html', {'memberList': memberList, 'message':message })
def member_list(request,message):
memberList = Member.objects.all()
return render_response(request, 'user/member_list.html', {'memberList': memberList, 'message':message })
urls.py
(r'^member/list/
What is the right way to do this ?
Thanks
, 'views.member_list'),
(r'^member/list/(?P<message>[-\w]+)/
What is the right way to do this ?
Thanks
, 'views.member_list'),
What is the right way to do this ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在视图签名中使用可选参数,以发挥您的优势。
只需像这样定义您的视图:
然后,如果 URL 中存在匹配项,它将被填充。如果没有,它仍然是
None
。(改编自这篇博文)
You can use optional arguments in your view signature to your advantage here.
Simply define your view like this:
And then if there's a match in the URL, it'll get filled in. If not, it will still be
None
.(Adapted from this blog post)