Django 1.6:在另一个模板中显示特定模型对象
Posted
技术标签:
【中文标题】Django 1.6:在另一个模板中显示特定模型对象【英文标题】:Django 1.6: Displaying a particular models Objects in another template 【发布时间】:2015-02-13 14:52:22 【问题描述】:如果我有一个一对多的关系,我有很多产品,而一些特定的产品将只与一个网站相关,那么我正在处理一个应用程序。
在我的主页上是我从我的网站模型中显示我列出的网站的地方一个产品页面(另一个模板),其中我的 产品模型 与该特定网站相关的所有对象仅显示。
这是我的情况的用户流程
主页 --> 点击网站 ('/browse/website_slug') ---> 转到 --> 产品页面(仅填充来自相关点击的 slug 的产品对象)
我的 product_extend 应用中的 Models.py
产品型号:
class Product(models.Model):
"""
The product structure for the application, the products we scrap from sites will model this and save directly into the tables.
"""
product_name = models.CharField(max_length=254, verbose_name=_('Name'), null=True, blank=True)
product_price = CurrencyField( verbose_name=_('Unit price') )
product_slug_url = models.URLField(max_length=200, null=True, blank=True)
product_category = models.CharField(max_length=254, blank=True, null=True)
product_img = models.ImageField('Product Image', upload_to='product_images', null=True, blank=True)
product_website_url = models.URLField(max_length=200, null=True, blank=True)
product_website_name = models.CharField(max_length=254, blank=True, null=True)
#For Admin Purposes, to keep track of new and old items in the database by administrative users
date_added = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date added'))
last_modified = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name=_('Last modified') )
#For Admin Purposes, to make sure an item is active by administrative users
active = models.BooleanField(default=True, verbose_name=_('Active') )
# Foreign Key
website = models.ForeignKey(Website, null=True, related_name='website_to_product')
网站模型
class Website(models.Model):
name = models.CharField(max_length=254, blank=True, null=True, unique=True)
description = models.TextField(null=True, blank=True)
website_slug = models.SlugField(verbose_name=_('Website Slug'), unique=True)
site_logo = models.ImageField('Websites Logo', upload_to='website_logo_images', null=True, blank=True)
menswear = models.BooleanField(default=False, verbose_name=_('Menswear'))
womenswear = models.BooleanField(default=False, verbose_name=_('Womenswear'))
active = models.BooleanField(default=True, verbose_name=_('Active'))
我的 product_extend 应用程序中的视图
view.py
class ProductView(ListView):
context_object_name = 'product_list'
template_name = 'product_extend/_productlist.html'
# queryset = ProductExtend.objects.filter(id=1)
model = Product
def get_context_data(self, **kwargs):
context = super(ProductView, self).get_context_data(**kwargs)
return context
class WebsiteView(ListView):
context_object_name = 'home'
template_name = 'homepage.html'
queryset = Website.objects.order_by('name')
model = Website
def get_context_data(self, **kwargs):
context = super(WebsiteView, self).get_context_data(**kwargs)
return context
模板
Homepage.html
% for object in home %
<li class="page_tiles-home home-website-reveal">
<a href="browse/website_slug" data-title="object.name" data-description="object.description">
<img src="MEDIA_URLobject.site_logo" />
</a>
</li>
% endfor %
Product.html
% for object in product_list %
<li class="col n-4">
<figure class="rollover site">
<div class="scrap-likes"><span class="icon-heart"></span></div>
<img src="object.product_img" >
<!-- <div class="scrap-from"> Scrapped from:<a class="scrap-site" target="_blank" href="object.product_website_url">object.product_website_name</a></div> -->
<div class="scrap-designer"> Scrapped from: <a class="scrap-site" target="_blank" href="object.product_website_url">object.product_website_name</a></div>
<div class="scrap-title">object.product_name , <span class="scrap-price">$object.product_price</span></div>
<a class="scrap-buy" target="_blank" href="object.product_slug_url">View Item</a>
</figure>
</li>
% endfor %
网址
我的应用 urls.py
urlpatterns = patterns('',
url(r"^$", WebsiteView.as_view(), name="home"),
url(r'^browse/', include('product_extend.urls')),
)
我的应用 product_extend urls.py
urlpatterns = patterns('',
??? No clue what to put ???
)
【问题讨论】:
【参考方案1】:你可以在product_extend urls.py中添加这个:
urlpatterns = patterns('',
url(r'^(?P<website_slug>[\w]+)$', ProductView.as_view(), name='products_list'),
)
然后在ProductView
中重写get_queryset
方法以使用website_slug
过滤查询集:
class ProductView(ListView):
context_object_name = 'product_list'
template_name = 'product_extend/_productlist.html'
# queryset = ProductExtend.objects.filter(id=1)
model = Product
def get_context_data(self, **kwargs):
context = super(ProductView, self).get_context_data(**kwargs)
return context
def get_queryset(self):
qs = super(ProductView, self).get_queryset()
return qs.filter(website__website_slug__exact=self.kwargs['website_slug'])
【讨论】:
【参考方案2】:看了两遍,想想自己想要的是:
url(r'^product/website/(?P<slug>)$', "your_view_to_peform_product_search_for_slug_website_here"),
在你看来“HTML”
href="product/website/ website.slug "
这样的……
【讨论】:
以上是关于Django 1.6:在另一个模板中显示特定模型对象的主要内容,如果未能解决你的问题,请参考以下文章