0

我正在尝试使用基于类的视图发送变量。下面是url文件的代码

from django.conf.urls import patterns, include, url
from myapp.views import foo

urlpatterns =  patterns('',
    (r'^/$', foo.as_views(template = 'home.html')),
    (r'^about/$', foo.as_views(template = 'about.html')),
)

如何在我的视图文件的 foo 类中访问它?我正在尝试做这样的事情:

return render(request, template_n)
4

1 回答 1

1

The parameters you pass to as_view are assigned as instance variables on your class based view so you can can self.template, i.e:

...
return render(request, self.template, {...})

As a sidenote, if you were using a named url patterns that captured a slug, for example:

url(r'^about/(?P<slug>[\w-]+)/$', foo.as_views(template='about.html')),

you would have access to the slug variable that is passed via the url by using the kwargs instance variable:

self.kwargs['slug']
于 2013-06-04T15:18:52.753 回答