help

17 views
Skip to first unread message

Tosin Ayoola

unread,
Jul 3, 2019, 7:26:22 PM7/3/19
to django...@googlegroups.com
Good day guyz
sorry i'm working on a django e-commerce project which i am new to, and i'm stuck, i wan have a page that will display the list of all for the list of all products under a particular category, which i have checked out on stackoverflow, i tried every suggestion and yet since not working below is my code hoping anyone can lead mr in d right direction

##View

def index(request):
products = Product.objects.all()
category = get_list_or_404(Category)
cat = Category.objects.filter(pk = id)
return render(request, 'shop/index.html', locals())

##product_list Page
<h2>Categories</h2>
{% if cat %}
<ul>
{% for c in cat %}
<li>
<a href="{% url 'product-list' %}"> {{c.name}}</a>
</li>
{% for product in cat.get_product %}
<p> {(product.name}}</p>
{% endfor %}
{% endfor %}
</ul>
{% else %}
<p> There's no item category yet
{% endif %}
</div>



Charlotte Wood

unread,
Jul 3, 2019, 7:33:25 PM7/3/19
to django...@googlegroups.com
What is the name of the field in your model?  ie:  Title, Name, etc....  so one option would be product.title   or product.name....

can you just put your model in here?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHLKn70PDKYwv30LhTsRaQEJ5V9mwE0xri-EdmwPp8ZoAC5nng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Charlotte Wood

unread,
Jul 3, 2019, 7:33:40 PM7/3/19
to django...@googlegroups.com
and, are you using a base.html?  and a form.html?  then customizing the template for the product_view?

Tosin Ayoola

unread,
Jul 3, 2019, 7:38:05 PM7/3/19
to django...@googlegroups.com
this' my model
from django.db import models
from django.shortcuts import reverse

class Category(models.Model):
name = models.CharField(max_length = 100, db_index = True)
slug = models.SlugField(max_length = 130, db_index= True, unique = True)
class Meta:
db_table = 'categories'
verbose_name = 'catergories'

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("category-detail", args=[self.slug])

def get_product(self):
return Product.objects.filter(category= self.name)


class Product(models.Model):
name = models.CharField(max_length = 100, db_index = True)
slug = models.SlugField(max_length= 100, db_index= True)
brand = models.CharField(max_length = 100)
available = models.BooleanField(default= True)
meta_keywords = models.CharField(max_length = 150, blank = True)
stock = models.PositiveIntegerField()
updated = models.DateTimeField(auto_now= True)
price = models.DecimalField(max_digits = 10, decimal_places = 2, default = 0.00)
image = models.ImageField(upload_to = 'product_imgs', blank = True)
best_seller = models.BooleanField(default=False)
category = models.ForeignKey(Category, on_delete = 'MODEL.CASCADE', related_name='product')
description = models.TextField()
created = models.DateTimeField()

class meta:
db_name = 'Product'
ordering = [' -created']
index_together = (('id', 'slug'),)

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("product-detail", args= [self.slug,
self.created.year,
self.created.strftime('%m'),
self.created.strftime('%d')])

Charlotte Wood

unread,
Jul 3, 2019, 9:08:58 PM7/3/19
to django...@googlegroups.com
OK, this is CLOSE and it may even work....maybe someone else can correct it, because I may have category/product backwards, and I put a filter in there so you can search for a product by attribute, but you'll have to tweak it...

category_view.html

{% extends "base.html" %}
{% block content %}
<h3 align="center"> Products in a Category </h3>
<form method="get">
    {{ object_list.form.as_p }}
  <button type="submit">Search</button>
</form>
{% for object in object_list.qs%}
{% include 'category-inline.html'%}
{% endfor %}
{% endblock %}




category_inline.html

<div class='col-80 col-md-100 mb-1 mx-auto'>
<div class='card'>
  <div class='card-body'> 
<h5 class = 'card-title'>       
<p>{{object.name}}-{{object.slug}}</p>      
{% for product in object.category.category.all %}          
{{product.name}}--{{product.slug}}--{{product.brand}}--
{{product.available}}{{product.stock}}                 
{% endfor %}


views.py

from .filters import ProductFilter

def category_view(request):  
my_title = "Products"  
qs = Category.objects.prefetch_related('category__category')  
category_list = ProductFilter(request.GET, queryset=qs)  
template_name = 'category_view.html'  
context = {'object_list': category_list,"title":my_title}  
return render (request, template_name, context)



filters.py


class ProductFilter(django_filters.FilterSet):
category__name = django_filters.CharFilter(lookup_expr='icontains',label='Product Name contains')
class Meta:      
model = Product
fields = {

Joe Reitman

unread,
Jul 4, 2019, 12:58:19 PM7/4/19
to Django users
I would suggest not using locals(). You only have three variables to pass through to your template. 

Tosin Ayoola

unread,
Jul 5, 2019, 2:35:45 AM7/5/19
to django...@googlegroups.com

Thanks it was helpful learned about django-filter,
But i just want it simple as in , i already have the list of categories displaying, i just need something that when the customer click on a particular category name the customer will get redirected to another page with all the list of products under that category



On Thu, Jul 4, 2019 at 12:33 AM Charlotte Wood <charlotte.wood@epiccharterschools.org> wrote:
and, are you using a base.html?  and a form.html?  then customizing the template for the product_view?



Charlotte Wood, MEd

Educator

(405) 578-5701

Zoom Meeting ID#: 4055785701

Zoom URL: https://epiccharterschools.zoom.us/j/2970513912

Classroom Google Site: https://sites.google.com/epiccharterschools.org/charlottewoodclassroom/home

Epic Technical Support: (405) 652-0935



Jordan McKesson Principal

405-749-4550 ext. 309

On Wed, Jul 3, 2019 at 6:32 PM Charlotte Wood <charlotte.wood@epiccharterschools.org> wrote:
What is the name of the field in your model?  ie:  Title, Name, etc....  so one option would be product.title   or product.name....

can you just put your model in here?


Charlotte Wood, MEd

Educator

(405) 578-5701

Zoom Meeting ID#: 4055785701

Zoom URL: https://epiccharterschools.zoom.us/j/2970513912

Classroom Google Site: https://sites.google.com/epiccharterschools.org/charlottewoodclassroom/home

Epic Technical Support: (405) 652-0935



Jordan McKesson Principal

405-749-4550 ext. 309

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

mail2pramo

unread,
Jul 5, 2019, 2:58:01 AM7/5/19
to django...@googlegroups.com
How to deploy my django project into the web server.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Manjunatha Sai Uppu

unread,
Jul 5, 2019, 8:56:01 AM7/5/19
to Django users
I am new to web development  in django , i have some questions that
1. Is it necessary to  learn html,css?
2. what are the advantages and disadvantages of django?
3. how it is useful?
4. any best resources that u can provide for me?
 thanks in advance...!

Jani Tiainen

unread,
Jul 5, 2019, 10:21:03 AM7/5/19
to django...@googlegroups.com
Hi. 

Please don't hijack other people threads but start a new one.


pe 5. heinäk. 2019 klo 15.55 Manjunatha Sai Uppu <manjunat...@gmail.com> kirjoitti:
I am new to web development  in django , i have some questions that
1. Is it necessary to  learn html,css?

No. Absolutely no. You can write plain backend code for example by using JSON respondses.

Though if you want to write full web sites HTML and CSS greatly helps.

2. what are the advantages and disadvantages of django?

A good friendly community. And it's written in Python. Best programming language ever.

3. how it is useful?

In my opionion it's very fast tool to make ideas to real dynamic web apps.

Also there are tons of Python libraries that you can leverage plus huge amount apps (kind of plugins) you can use in you Django projects to help making things even faster.

4. any best resources that u can provide for me?

Do tutorials. Django Girls do provide excellent tutorial to get started.

 thanks in advance...!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages