Home > Blockchain >  Django - Display list of category belonging to each card
Django - Display list of category belonging to each card

Time:04-01

I have a Giftcard model, and Category model which holds all category, rate and term for each card. It has a foreign key back to Giftcard.

I just want to be able to list category, rate and terms for each Giftcard. As the code is right now, it displays the displays same category, rate and terms for all Giftcard.

model

class Giftcard(models.Model):
name = models.CharField(max_length=100, unique=True)
card_image = models.ImageField(upload_to='Giftcard', blank=False)
date = models.DateTimeField(auto_now_add=True)
publish = models.BooleanField(default=False)



class Category(models.Model):
category = models.CharField(max_length=250)
name = models.ForeignKey(Giftcard, on_delete=models.CASCADE, related_name="specs")
rate = models.IntegerField()
terms = models.TextField()

Here is my views

def giftcard(request):
giftcards = Giftcard.objects.filter(publish=True)

context = {
    'giftcards': giftcards,
}

return render(request, 'dashboard/giftcard.html', context)

Here is my template

{% for giftcard in giftcards %}
          <!-- Card -->
          <div >
            <a type="button" >
              <img  src="{{ giftcard.card_image.url }}">
            </a>

            <div >
              <div >
                <div >
                  <div ></div>
                  <div >
                    <div >
                      <div >
                        <div >
                          <img  src="{{ giftcard.card_image.url }}">
                          <p >Select the card category and the amount.</p>
                        </div>

                        <div >
                          <form >
                            <div >
                              <label for="card-category">Card category</label>
                              <input list="category"  name="category" id="card_category"
                                placeholder="Select card category">

                              {% for spec in giftcard.specs.all %}
                              <datalist id="category">
                                <option value="{{ spec.category }}">{{ spec.category }}</option>
                              </datalist>
                              {% endfor %}
                            </div>

                            <div >
                              <label for="Amount">Amount</label>
                              <div >
                                <input type="text"  id="amount"
                                  placeholder="Please enter amount">
                                <span >#100,000</span>
                              </div>
                            </div>

                            <div >
                              {% for spec in giftcard.specs.all %}
                              <label for="rate">Current rate - {{ spec.rate }}</label>
                              {% endfor %}
                            </div>

                            <div >
                              <button type="button" >Proceed</button>
                            </div>
                          </form>
                        </div>
                      </div>
                    </div>

                  </div>
                


                <div  id="terms" tabindex="-1" role="dialog"
                  aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                  <div  role="document">
                    <div >
                      <div >
                        <h4 >Trading terms</h4>
                        {% for spec in giftcard.specs.all %}
                        <p >
                          {{ spec.terms }}
                        </p>
                        {% endfor %}

                        <div >
                          <button type="button" >Proceed</button>
                        </div>

                      </div>
                    </div>
                  </div>
                </div>
                </div>
              </div>
            </div>
          </div>
          {% endfor %}

CodePudding user response:

If, as you say in one of your comments, "it won't work that way because a giftcard can have more than one category" and "No, Category cannot have more than one Giftcard", then your original models are correct, with ForeignKey in the Category class. Second, I do not know what exactly is going on, perhaps add screenshots of what you are getting that is wrong, because I just tried your original code out, and it works, with the modifications to the datalist suggested by @Sunderam. Test this out in your html template:

{% for giftcard in giftcards %}
    {% for spec in giftcard.specs.all %}
        {{ spec.terms }}<br>
        {{ spec.category }}<br>
        {{ spec.rate }}<br>
    {% endfor %}
{% endfor %}

When I did this, I am seeing different terms, categories and rates for each. Again, is this not what you want?

Edit
You must understand that the datalist will have all the spec values, and one shows up when you start typing in the input box. for example, if you type c, then the list will pop up with all categories that begin with c.

Edit
Make sure the datalist has been changed to

<div >
  <label for="card-category">Card category</label>
  <input list="category"  name="category" id="card_category"
    placeholder="Select card category">

  {% for spec in giftcard.specs.all %}
  <datalist id="category">
    <option value="{{ spec.category }}">{{ spec.category }}</option>
  </datalist>
  {% endfor %}
</div>

After all, you want only one <datalist> element, and then many <option>s.

CodePudding user response:

From django-doc,you have to make ForeginKey field in your main Giftcard table which will do ManyToOne relation with Category table.

Try this:

models.py

from django.db import models


class Category(models.Model):
    category = models.CharField(max_length=250)
    rate = models.IntegerField()
    terms = models.TextField()

    def __str__(self):
        return self.category


class Giftcard(models.Model):
    name = models.CharField(max_length=100, unique=True)
    card_image = models.ImageField(upload_to='Giftcard/', blank=False)
    date = models.DateTimeField(auto_now_add=True)
    publish = models.BooleanField(default=False)
    gift_category = models.ForeignKey(Category, on_delete=models.CASCADE)

views.py

def giftcard(request):
    giftcards = Giftcard.objects.filter(publish=True)

    context = {
        'giftcards': giftcards
    }
    return render(request, 'dashboard/giftcard.html', context)

Now, you can access all the properties of Category table in Giftcard items in your template file, and a giftcard can have more than one category, it is called ManyToOne relation,

Note: you can also use choices, which is much better than making table, you can see that too here in django-doc.

Edit

Do this in datalist

<datalist id="category">
    {% for spec in giftcard.specs.all %}

        <option value="{{ spec.category }}">{{ spec.category }}</option>
    {% endfor %}

</datalist>
  • Related