makeabilitylab / makeabilitylabwebsite

The Makeability Lab website
https://makeabilitylab.cs.washington.edu
MIT License
9 stars 65 forks source link

Video object has no attribute project #1187

Closed jonfroehlich closed 1 month ago

jonfroehlich commented 1 month ago

Error on index.html load

DEBUG 2024-09-24 08:54:12,815 base _resolve_lookup 770 139854127036096 Exception while resolving variable 'project' in template 'website/index.html'.
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/django/template/base.py", line 880, in _resolve_lookup
    current = current[bit]
              ~~~~~~~^^^^^
TypeError: 'Video' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/django/template/base.py", line 890, in _resolve_lookup
    current = getattr(current, bit)
              ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Video' object has no attribute 'project'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/django/template/base.py", line 896, in _resolve_lookup
    current = current[int(bit)]
                      ^^^^^^^^
ValueError: invalid literal for int() with base 10: 'project'

During handling of the above exception, another exception occurred:

I isolated the error to the following in display_video_snippet.html

 {% if video.project %}
          |<a href="{% url 'website:project' video.project.short_name %}">{{ video.project.name }}</a>
      {% endif %}

At some point, we allowed videos to be associated with multiple videos, so video.project no longer exists. In fact, it's commented out in the Video model:

class Video(models.Model):
    video_url = models.URLField(blank=True, null=True)
    video_preview_url = models.URLField(blank=True, null=True)
    title = models.CharField(max_length=255)
    caption = models.CharField(max_length=255, blank=True, null=True)
    date = models.DateField(null=True)

    projects = models.ManyToManyField('Project', blank=True, null=True, related_name='videos')
    #project = models.ForeignKey('Project', blank=True, null=True, on_delete=models.SET_NULL)

So, the solution is something like we have in display_pub_snippet:

{% for project in video.projects.all %}
        {% if forloop.first and video.projects.all|length > 0 %}| {% endif %}
        {% if project.can_show_online %}
            <a href="{% url 'website:project' project.short_name %}">{{ project.name }}</a>{% if not forloop.last %} • {% endif %}
        {% endif %}
      {% endfor %}
jonfroehlich commented 1 month ago

Appears to work!