I have a custom class-based view
我有一个基于类的自定义视图
# myapp/views.py
from django.views.generic import *
class MyView(DetailView):
template_name = 'detail.html'
model = MyModel
def get_object(self, queryset=None):
return queryset.get(slug=self.slug)
I want to pass in the slug parameter (or other parameters to the view) like this
我想将slug参数(或其他参数)像这样传递给视图
MyView.as_view(slug='hello_world')
Do I need to override any methods to be able to do this?
我是否需要重写任何方法才能做到这一点?
94
If your urlconf looks something like this:
如果你的urlconf看起来是这样的:
url(r'^(?P<slug>[a-zA-Z0-9-]+)/$', MyView.as_view(), name = 'my_named_view')
then the slug will be available inside your view functions (such as 'get_queryset') like this:
然后,段塞将在您的视图函数(如“get_queryset”)中可用,如下所示:
self.kwargs['slug']
64
Every parameter that's passed to the as_view
method is an instance variable of the View class. That means to add slug
as a parameter you have to create it as an instance variable in your sub-class:
传递给as_view方法的每个参数都是View类的实例变量。这意味着要添加slug作为参数,必须在子类中创建一个实例变量:
# myapp/views.py
from django.views.generic import DetailView
class MyView(DetailView):
template_name = 'detail.html'
model = MyModel
# additional parameters
slug = None
def get_object(self, queryset=None):
return queryset.get(slug=self.slug)
That should make MyView.as_view(slug='hello_world')
work.
这将使view .as_view(slug='hello_world')工作。
If you're passing the variables through keywords, use what Mr Erikkson suggested: https://stackoverflow.com/a/11494666/9903
如果您通过关键字传递变量,请使用Erikkson建议的:https://stackoverflow.com/a/11494666/9903。
16
It's worth noting you don't need to override get_object()
in order to look up an object based on a slug passed as a keyword arg - you can use the attributes of a SingleObjectMixin
https://docs.djangoproject.com/en/1.5/ref/class-based-views/mixins-single-object/#singleobjectmixin
值得注意的是,您不需要重写get_object()来查找作为关键字arg传递的蛞蝓的对象——您可以使用https://docs.djangoproject.com/en/1.5/ref/class- bass/mixins-singles-singles-singles-single -object/# objectmixin中的SingleObjectMixin属性
# views.py
class MyView(DetailView):
model = MyModel
slug_field = 'slug_field_name'
slug_url_kwarg = 'model_slug'
context_object_name = 'my_model'
# urls.py
url(r'^(?P<model_slug>[\w-]+)/$', MyView.as_view(), name = 'my_named_view')
# mymodel_detail.html
{{ my_model.slug_field_name }}
(both slug_field
and slug_url_kwarg
default to 'slug'
)
(slu_field和slu_url_kwarg默认为“蛞蝓”)
13
If you want to add an object to the context for the template you can override get_context_data
and add to its context. The request is also a part of self in case you need the request.user.
如果要为模板向上下文添加对象,可以覆盖get_context_data并添加到其上下文。如果需要request.user,请求也是self的一部分。
def get_context_data(self, **kwargs):
context = super(MyTemplateView, self).get_context_data(**kwargs)
if 'slug' in self.kwargs:
context['object'] = get_object_or_404(MyObject, slug=self.kwargs['slug'])
context['objects'] = get_objects_by_user(self.request.user)
return context
8
You can pass parameters from urls.py https://docs.djangoproject.com/en/1.7/topics/http/urls/#passing-extra-options-to-view-functions
您可以从url传递参数。py https://docs.djangoproject.com/en/1.7/topics/http/urls/ passing-extra-options-to-view-functions
This also works for generic views. Example:
这也适用于通用视图。例子:
url(r'^$', views.SectionView.as_view(), { 'pk': 'homepage', 'another_param':'?'}, name='main_page'),
In this case the parameters passed to the view should not necessarily be instance variables of the View class. Using this method you don't need to hardcode default page name into YourView model, but you can just pass it as a parameter from urlconf.
在这种情况下,传递给视图的参数不一定是视图类的实例变量。使用此方法,不需要将默认页名硬编码到视图模型中,但可以将其作为参数从urlconf传递。
7
As stated by Yaroslav Nikitenko, if you don't want to hardcode a new instance variable to the View class, you can pass extra options to view functions from urls.py
like this:
如Yaroslav Nikitenko所说,如果你不想硬编码一个新的实例变量到视图类,你可以通过url传递额外的选项来查看函数。py是这样的:
url(r'^$', YourView.as_view(), {'slug': 'hello_world'}, name='page_name')
I just wanted to add how to use it from the view. You can implement one of the following methods:
我只是想从视图中添加如何使用它。你可采用以下方法之一:
# If slug is optional
def the_function(self, request, slug=None):
# use slug here
# if slug is an optional param among others
def the_function(self, request, **kwargs):
slug = kwargs.get("slug", None)
other_param = kwargs.get("other_param", None)
# If slug is required
def the_function(self, request, slug):
# use slug here
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/07/15/4b635331bee62fea2d41e1ae08fcdcce.html。