vendredi 14 novembre 2014

Django : Comment afficher tous les produits dans une catégorie particulière - Stack Overflow


In my admin page I have 2 apps. One for the Category and the other is for the Products.


In the category I have PS3 and PS4, and in the products..well.. I have the products.


What I want to do is display all of the products for the category. How would I do this? On my html pages, I have a menu bar: Home, PS3 and PS4. When someone clicks on the PS3 or PS4 tab, I want it to display the products for each tab, instead of creating a seperate category page and details page.


In my database I have the following fields for Category: name. In the Products I have the following fields: title, description, image, price, stock.


I have all the pages working, but I just need to display the products. I hope I am clear. I cannot do this. I will show my code..


MODELS.PY..


from django.db import models

# Create your models here.

class Category(models.Model):
name = models.CharField(max_length=30)

class Meta:
ordering = ["name"]

def __str__(self):
return self.name

class Products(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
description = models.TextField()
stock = models.IntegerField()
price = models.DecimalField(max_digits=6, decimal_places=2)
image = models.ImageField(upload_to="product_images", blank=True)


class Meta:
ordering = ["title", "description", "image", "price", "stock"]

def __str__(self):
return self.title

IN MY VIEWS.PY...


from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponse
from category.models import *

def home(request):
return render_to_response("category/index.html",locals(),context_instance=RequestContext(request))

def ps3(request):
return render_to_response("category/ps3.html",locals(),context_instance=RequestContext(request))

def ps4(request):
return render_to_response("category/ps4.html",locals(),context_instance=RequestContext(request))

IN MY URLS.PY..


from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',

url(r'^$', 'category.views.home', name='home'),
url('^ps3/', 'category.views.ps3', name ='ps3'),
url('^ps4/', 'category.views.ps4', name ='ps4'),
url(r'^admin/', include(admin.site.urls)),

)



The context_instance argument is the important part here, as you can see, it's a RequestContext class, this class takes a request as its first argument, and you can pass any data you want as a dictionary in the second argument:


c = RequestContext(request, {
'foo': 'bar',
})

You need to make use of this, to pass the data you want to use in your templates. And please, learn to Don’t repeat yourself (DRY), and apply that in your URLs/Views.




You can use a helper method to achieve this.


To get a list of products specific to a category:


class Category(models.Model):
name = models.CharField(max_length=30)

class Meta:
ordering = ["name"]

def __str__(self):
return self.name

def products(self):
return self.products_set.all()

And in the template, you would just do


{% for category in categories %}
{% category.name %}
{% for product in category.products %}
{{ product }}
{% empty %}
No products found for this category
{% endfor %}
{% endfor %}

You can also just do this:


given an object instance category,


{% for product in category.product_set.all %}
{{product}}
{% endfor %}

Read more on lookups that span relationships here


Further, in your views, you have locals(), but nothing being sent into the context. You might need something like:


def ps3(request):
try:
category = Category.objects.get(name='ps3')
products = category.products()
except:
products = None

return render_to_response("category/index.html", {'products': products}, context_instance=RequestContext(request))

and in the template:


{% for product in products %}
{{product}}
{% empty %}
No products found
{% endfor %}

Now you need to customize it to your specific needs. This is one way of doing things.



In my admin page I have 2 apps. One for the Category and the other is for the Products.


In the category I have PS3 and PS4, and in the products..well.. I have the products.


What I want to do is display all of the products for the category. How would I do this? On my html pages, I have a menu bar: Home, PS3 and PS4. When someone clicks on the PS3 or PS4 tab, I want it to display the products for each tab, instead of creating a seperate category page and details page.


In my database I have the following fields for Category: name. In the Products I have the following fields: title, description, image, price, stock.


I have all the pages working, but I just need to display the products. I hope I am clear. I cannot do this. I will show my code..


MODELS.PY..


from django.db import models

# Create your models here.

class Category(models.Model):
name = models.CharField(max_length=30)

class Meta:
ordering = ["name"]

def __str__(self):
return self.name

class Products(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
description = models.TextField()
stock = models.IntegerField()
price = models.DecimalField(max_digits=6, decimal_places=2)
image = models.ImageField(upload_to="product_images", blank=True)


class Meta:
ordering = ["title", "description", "image", "price", "stock"]

def __str__(self):
return self.title

IN MY VIEWS.PY...


from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponse
from category.models import *

def home(request):
return render_to_response("category/index.html",locals(),context_instance=RequestContext(request))

def ps3(request):
return render_to_response("category/ps3.html",locals(),context_instance=RequestContext(request))

def ps4(request):
return render_to_response("category/ps4.html",locals(),context_instance=RequestContext(request))

IN MY URLS.PY..


from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',

url(r'^$', 'category.views.home', name='home'),
url('^ps3/', 'category.views.ps3', name ='ps3'),
url('^ps4/', 'category.views.ps4', name ='ps4'),
url(r'^admin/', include(admin.site.urls)),

)


The context_instance argument is the important part here, as you can see, it's a RequestContext class, this class takes a request as its first argument, and you can pass any data you want as a dictionary in the second argument:


c = RequestContext(request, {
'foo': 'bar',
})

You need to make use of this, to pass the data you want to use in your templates. And please, learn to Don’t repeat yourself (DRY), and apply that in your URLs/Views.



You can use a helper method to achieve this.


To get a list of products specific to a category:


class Category(models.Model):
name = models.CharField(max_length=30)

class Meta:
ordering = ["name"]

def __str__(self):
return self.name

def products(self):
return self.products_set.all()

And in the template, you would just do


{% for category in categories %}
{% category.name %}
{% for product in category.products %}
{{ product }}
{% empty %}
No products found for this category
{% endfor %}
{% endfor %}

You can also just do this:


given an object instance category,


{% for product in category.product_set.all %}
{{product}}
{% endfor %}

Read more on lookups that span relationships here


Further, in your views, you have locals(), but nothing being sent into the context. You might need something like:


def ps3(request):
try:
category = Category.objects.get(name='ps3')
products = category.products()
except:
products = None

return render_to_response("category/index.html", {'products': products}, context_instance=RequestContext(request))

and in the template:


{% for product in products %}
{{product}}
{% empty %}
No products found
{% endfor %}

Now you need to customize it to your specific needs. This is one way of doing things.


0 commentaires:

Enregistrer un commentaire