Hi,
Not sure if anyone is familiar with Django Unicorn that can help.
Im receiving an error “Checksum does not match” when calling a Unicorn function a second time.
Code is below that is causing it
from django_unicorn.components import UnicornView
from mealtracker.models import PersonMacros, User, PersonDailyFood, FoodMacros
from datetime import datetime
class DisplayfooddiaryView(UnicornView):
name = 'Displayfooddiary'
calories= None
DailyMealSpecificDiary : PersonDailyFood = None
DiaryEntry : PersonDailyFood = None
user: str = ""
meal: str = ""
meal_pk = None
def mount(self):
print("Initial request that rendered the component", self.request)
user = self.request.user
now = datetime.now()
PersonMacroInstance = PersonMacros.objects.get(name=user)
self.DailyMealSpecificDiary = PersonDailyFood.objects.filter(user=PersonMacroInstance).filter(date=now).filter(meal="Breakfast")
def edit(self, pk, mealType):
print(self)
print(pk)
self.DiaryEntry = PersonDailyFood.objects.get(pk=pk)
self.call("addFoodItem", mealType)
<button unicorn:click="edit({{item.pk}}, 'Breakfast')" class="btn btn-outline-dark btn-sm">Save</button>
This works the first time but then receives the error on 2nd time.
Also this is calling a javascript function to open a bootstrap modal but I was wondering if there is a way in Unicorn or Django to populate the modal from the views as oppose to Javascript
Thanks in advance
Usually this is because a class-based variable is set after the first click, but is the wrong type for the second click. I can’t replicate this right now, but will be able to try in a few days. I’ll let you know what I find if you can’t figure it out before then.
Hi Adam, thanks for the response, I get what your saying for that, although have changed the way I do it now so no need to worry.
If i get back to doing something similar will report back
Hey @adamghill ,
getting something similar again happening. I imagine its something really simple im doing wrong.
I have the below component, which is used 4 times on the page.
I’m trying to get the add/ edit to work consistently to redirect to the edit item page, but randomly receive the below error:
python file:
from django.shortcuts import redirect
from django_unicorn.components import LocationUpdate, UnicornView
from mealtracker.models import PersonMacros, User, PersonDailyFood, FoodMacros
from datetime import datetime
class DisplayfooddiaryView(UnicornView):
name = 'Displayfooddiary'
calories= None
DailyMealSpecificDiary : PersonDailyFood
DiaryEntry : PersonDailyFood
user: str = ""
meal: str = ""
meal_pk = None
mealType: str
def mount(self):
print("Initial request that rendered the component", self.request)
self.mealType = self.component_kwargs["mealType"]
user = self.request.user
now = datetime.now()
PersonMacroInstance = PersonMacros.objects.get(name=user)
self.DailyMealSpecificDiary = PersonDailyFood.objects.filter(user=PersonMacroInstance).filter(date=now).filter(meal=self.mealType)
def edit(self, pk):
print(self)
print(pk)
self.DiaryEntry = PersonDailyFood.objects.get(pk=pk)
self.reset()
return redirect(f"../foodDiaryItem/{pk}")
def delete(self):
PersonDailyFood.objects.delete(pk=self.pk)
PersonMacroInstance = PersonMacros.objects.get(name=self.request.user)
now = datetime.now()
self.DailyMealSpecificDiary = PersonDailyFood.objects.filter(user=PersonMacroInstance).filter(date=now).filter(meal=self.mealType)
def add(self):
print(self)
self.reset()
return redirect(f"../addFoodItem/{self.mealType}")
Error Received:
self.mealType = self.component_kwargs["mealType"]
KeyError: ‘mealType’
HTML file
<div class="accordion-item">
<h2 class="accordion-header" id="heading{{ mealType }}">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse{{mealType}}" aria-expanded="false" aria-controls="collapse{{mealType}}">
{{ mealType }}
</button>
</h2>
<div id="collapse{{mealType}}" class="accordion-collapse collapse" aria-labelledby="heading{{ mealType }}" data-bs-parent="#accordionExample" style="">
<div class="accordion-body" >
<table class="table table-hover" id="{{mealType}}-table">
<thead>
<tr>
<th scope="col">Food</th>
<th scope="col">Quantity</th>
<th scope="col">Calories</th>
</tr>
</thead>
<tbody unicorn:model="DailyMealSpecificDiary">
{% for item in DailyMealSpecificDiary %}
<tr>
<td>{{item.foodName}} </td>
<td>{{item.quantity}}</td>
<td>{{item.calories}}</td>
<td>
<button unicorn:click="edit({{item.pk}})" class="btn btn-outline-dark btn-sm">Save</button>
<button unicorn:click="delete()" class="btn btn-outline-dark btn-sm"><i class="fa-solid fa-1x fa-trash-can"></i></button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button class="btn btn-outline-secondary" unicorn:click="add()">Add Food</button>
</div>
</div>
</div>
thanks in advance!