具有多个参数的 Django 通用视图
是否可以在 URL 映射中使用带有附加参数的通用视图 - 即我得到以下模型:
class Route(models.Model):
area = models.ForeignKey(Area)
slug = models.SlugField(null=True,blank=True)
@models.permalink
def get_absolute_url(self):
return ('route_details', (), {'area': self.area.slug,
'slug': self.slug})
这个 URL 映射:
url(r'^(?P<area>[-\w]+)/(?P<slug>[-\w]+)/$','routes.views.route_details',
name='route_details')
这个简单的视图:
def route_details(self, area, slug):
route = get_object_or_404(Route, slug=slug)
return render_to_response("routes/route_details.html", {'route': route})
如您所见,我实际上只是通过路由的 slug 来识别路由区域 slug 只是为了塑造 url(例如routes/central-park/rat-rock
)。我可以只使用通用视图做同样的事情吗?
Is it possible to use a generic view with additional parameters in the URL mapping - i.e. I got the following model:
class Route(models.Model):
area = models.ForeignKey(Area)
slug = models.SlugField(null=True,blank=True)
@models.permalink
def get_absolute_url(self):
return ('route_details', (), {'area': self.area.slug,
'slug': self.slug})
This URL-Mapping:
url(r'^(?P<area>[-\w]+)/(?P<slug>[-\w]+)/
And this trivial view:
def route_details(self, area, slug):
route = get_object_or_404(Route, slug=slug)
return render_to_response("routes/route_details.html", {'route': route})
As you can see, I'm actually identifying the route just by the route's slug and the area slug is just to shape the url (e.g. routes/central-park/rat-rock
). Can I do the same just using a generic view?
,'routes.views.route_details',
name='route_details')
And this trivial view:
As you can see, I'm actually identifying the route just by the route's slug and the area slug is just to shape the url (e.g. routes/central-park/rat-rock
). Can I do the same just using a generic view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,像这样的东西:
应该可以工作。
请务必将 template_object_name 设置为“route”或在模板中使用“object”。
Sure, something like this:
Should just work.
Be sure to either set template_object_name to "route" or use "object" in template.