Reputation: 31
I want to have a button which links to page make-appointment
template.html
{% for key, value in closest_free_spots.items %}
...
<a href="{% url 'make-appointment' value.pk %}"><button type="button">{{ value.datetime }}</button></a>
urls.py
url(r'^umowienie-spotkania/(?P<pk>\d+)/$', TestPy.views.MakingAppointmentView.as_view(),
name='make-appointment'),
However when I try to launch it, I get error:
Reverse for 'make-appointment' with arguments '('',)' not found. 1 pattern(s) tried: ['umowienie-spotkania/(?P<pk>\\d+)/$']
It looks like the value.pk is not correct, but when I print it one line above, it gives correct value (i.e. 4).
What is the problem? What I'm missing?
closest_free_spots definition - value is object from CalendarFreeSlot model
closest_free_spots = {
lawyer: TestPy.models.CalendarFreeSlot.objects.filter(Q(lawyer_id=lawyer) & Q(is_available=True)).first() for
lawyer in object_list}
models.py
class CalendarFreeSlot(models.Model):
lawyer_id = models.ForeignKey('MyUser', on_delete=models.PROTECT)
datetime = models.DateTimeField()
is_available = models.BooleanField(default=True)
Upvotes: 0
Views: 45
Reputation: 308839
Your closest_free_spots
dict comprehension uses CalendarFreeSlot.objects.filter(...).first()
, which can return None
. This leads to the with arguments '('',)'
part of the error message.
Checking {{ value.pk }}
in the template was a good idea, but it looks like you missed that there isn't a value for every item in the dictionary.
You could handle this by checking {% if value %}
in the template.
{% for key, value in closest_free_spots.items %}
{% if value %}
<a href="{% url 'make-appointment' value.pk %}"><button type="button">{{ value.datetime }}</button></a>
{% else %}
{{ key }} has not free spots
{% endif %}
{% endfor %}
Upvotes: 1