Please, I need help with my Django project. I'm having an issue displaying the card rate according to the selected card category. I have been having this Issue for a while now please I need help.
Here is my my view:
@login_required(login_url='/Authentication/login')
def giftcard(request):
giftcards = Giftcard.objects.filter(publish=True)
context = {
'giftcards': giftcards,
'categories': categories,
}
return render(request, 'dashboard/giftcard.html', context)
Here is my models and I link them using ForeignKey:
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)
card = models.ForeignKey(Giftcard, on_delete=models.CASCADE)
class CardRate(models.Model):
rate = models.IntegerField()
card_category = models.ForeignKey(Category, on_delete=models.CASCADE)
Here is my template, i think am writing the wrong code here:
{% for giftcard in giftcards %}
<!-- Card -->
<div >
<div >
<div >
<div ></div>
<div >
<div >
<div >
<div >
<img style="width: 40rem;" 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>
<select id="category" aria-label="Default select example">
{% for spec in giftcard.category_set.all %}
<option value="{{ spec.category }}">{{ spec.category }}</option>
{% endfor %}
</select>
</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.category_set.all %}
<label for="rate">Current rate - {{ spec.rate }}</label>
{% endfor %}
</div>
<div >
<button type="button" >Proceed</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
CodePudding user response:
You need to put the foreign key in the gift card class not in the category, because each card has a category and each category has one or many cards, so you need to do that in your models.py :
class Category(models.Model):
category = models.CharField(max_length=250)
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)
Category = models.ForeignKey(Category, on_delete=models.CASCADE)
CodePudding user response:
According to your comment you can try this add related_name to your card field :
class Category(models.Model):
category = models.CharField(max_length=250)
card = models.ForeignKey(Giftcard, on_delete=models.CASCADE,
related_name="categories")
and in your view :
@login_required(login_url='/Authentication/login')
def giftcard(request):
giftcards = Giftcard.objects.filter(publish=True)
categories = giftcards.categories.all()
context = {
'giftcards': giftcards,
'categories': categories,
}
return render(request, 'dashboard/giftcard.html', context)
In your html page :
{% for giftcard in giftcards %}
<!-- Card -->
<div >
<div >
<div >
<div ></div>
<div >
<div >
<div >
<div >
<img style="width: 40rem;" 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>
<select id="category" aria-label="Default select example">
{% for spec in categories %}
<option value="{{ spec.category }}">{{ spec.category }}</option>
{% endfor %}
</select>
</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 categories %}
<label for="rate">Current rate - {{ spec.rate }}</label>
{% endfor %}
</div>
<div >
<button type="button" >Proceed</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}