Working on an application were I have a One to Many relationship where I have many Products and a few particular products will be related to only one Website.
On my Home page is where I display my listed sites from my Website Model I would like to show products for when the user clicks on anyone of the slugs on my Homepage the are redirected to go into a product page ( another template ) where I have all of the objects related from my Product Model to that particular website to display only.
Here is a User flow of my situation
Homepage --> click on website ('/browse/website_slug') ---> Go To --> Product page (filled with only Product Objects from related clicked slug)
Product Model:
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')
Website Model
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'))
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 alt="{{object.name}}" src="{{MEDIA_URL}}{{object.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}}" width="470" height="700">
<!-- <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 %}
my apps urls.py
urlpatterns = patterns('',
url(r"^$", WebsiteView.as_view(), name="home"),
url(r'^browse/', include('product_extend.urls')),
)
my apps product_extend urls.py
urlpatterns = patterns('',
??? No clue what to put ???
)
You can add this in product_extend urls.py :
urlpatterns = patterns('',
url(r'^(?P<website_slug>[\w]+)$', ProductView.as_view(), name='products_list'),
)
Then in ProductView
override the get_queryset
method to use the website_slug
for filtering the queryset:
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'])
after reading twice, think what you want is:
url(r'^product/website/(?P<slug>)$', "your_view_to_peform_product_search_for_slug_website_here"),
and in your view "HTML"
href="product/website/{{ website.slug }}"
something like this...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.