Home > Software design >  How do I set ProductDetail Page for different categories of product In Django?
How do I set ProductDetail Page for different categories of product In Django?

Time:12-19

I am new to Django. I am building a web store app.

Case 1: I have a category for Phones with these Model: 'title', 'description', 'price', 'front camera', 'back camera',

Case 2: I have another category for accessories with these Model: 'title', 'price', 'description', 'type'

The problem: They both share the same ProductDetail Page...accessories detail is showing front and back camera as features which is akward although both are set to none.

The Ask:

How can I render different ProducDetail Page for different Categories

ProductDetail page for Accessories ProductDetail page for Phones

Below is my template-

 <div >
                    <h4 >{{ object.title }}</h4>
                    <h5 >&#8358;{{object.price}}</h5>
                    <h6 ><i ></i> {{ object.location }}
                    </h6>
                    <small >Posted on {{ object.updated_on }}</small>
                    <hr>
                    <p>BRAND: <span > {{ object.brand }} </span></p>
                    <p>CONDITION: <span > {{ object.condition }} </span></p>
                    <p>SCREEN SIZE: <span > {{ object.screensize }} </span></p>
                    <p>DISPLAY: <span > {{ object.display }} </span></p>
                    <p>FRONT CAMERA: <span > {{ object.frontcamera }} </span></p>
                    <p>BACK CAMERA: <span > {{ object.backcamera }} </span></p>
                    <p>OPERATING SYSTEM: <span > {{ object.operating_system }} </span></p>
                    <p>COLOR: <span > {{ object.color}} </span></p>
                    <p>RAM: <span > {{ object.ram }} </span></p>
                    <p>STORAGE: <span > {{ object.storage }} </span></p>
                    <hr>
                    <p>{{ object.description }}</p>
                    <hr>

                    <div >
                        <button id="phonebutton" >View phone number</button>
                        <p>
                            <a href="tel: {{ object.agent.profile.phone }}" id="phonelink" >
                                {{ object.agent.profile.phone }} </a>
                        </p>
                    </div>
                    <div>
                        {% if object.agent == user %}
                        <a 
                            href="{% url 'shopitapp:product-update' object.id %}">Update</a>
                        <a 
                            href="{% url 'shopitapp:product-delete' object.id %}">Delete</a>
                        {% endif %}
                    </div>
                </div>

This is my View >


class ProductDetailView(DetailView):
    model = Product

 

class ProductCreateView(LoginRequiredMixin, CreateView):
    model = Product
    fields = ['title', 'description', 'location', 'price', 'category', 'brand', 'condition',
              'screensize', 'display', 'frontcamera', 'backcamera', 'operating_system', 'color', 'ram', 'storage', 'product_image1', 'product_image2', 'product_image3']

    def form_valid(self, form):
        form.instance.agent = self.request.user
        return super().form_valid(form)


class ProductUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Product
    fields = ['title', 'description', 'location', 'price', 'category', 'brand', 'condition',
              'screensize', 'display', 'frontcamera', 'backcamera', 'operating_system', 'color', 'ram', 'storage', 'product_image1', 'product_image2', 'product_image3']

    def form_valid(self, form):
        form.instance.agent = self.request.user
        return super().form_valid(form)

    def test_func(self):
        product = self.get_object()
        if self.request.user == product.agent:
            return True
        return False


class ProductDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Product
    success_url = '/'

    def test_func(self):
        product = self.get_object()
        if self.request.user == product.agent:
            return True
        return False


CodePudding user response:

you can use jinja in your template

{% if object.frontcamera is not none %}   
    <p>FRONT CAMERA: <span > {{ object.frontcamera }} </span></p>
{% else %}
    do nothing
{% endif %}

{% if object.backcamera is not none %}   
    <p>FRONT CAMERA: <span > {{ object.backcamera }} </span></p>
{% else %}
    do nothing
{% endif %}
  • Related