local variable 'Gallery' referenced before assignment

25 views
Skip to first unread message

Ross Symonds

unread,
Jun 9, 2020, 9:29:19 PM6/9/20
to Django Photologue
I am trying to show photos from my galleries at http://localhost:8000/photos/about/.  But I am getting the error message 


UnboundLocalError at /photos/about/

local variable 'Gallery' referenced before assignment

Does anyone know what the error message means?

urls.py
path('about/', views.about, name='gallery-about'),

views.py
def about(request):
Gallery = Gallery.objects.filter(photos__approved=True)
return render(request, 'blog/about3.html')

Ross Symonds

unread,
Jun 9, 2020, 9:37:45 PM6/9/20
to Django Photologue
I changed it to 

gallery = Gallery.objects.filter(photos__approved=True)

And now I am getting a new error message

Related Field got invalid lookup: approved

Ross Symonds

unread,
Jun 9, 2020, 10:09:52 PM6/9/20
to Django Photologue
I am now being shown my three galleries. Instead of the contents of the galleries. 



gallerytest.html
{% extends "blog/base.html" %}
{% block content %}
<h1>Gallery Test</h1>

{% for photo in gallery %}
<a href="{{ photo.get_absolute_url }}">
<img src="{{ photo.get_thumbnail_url }}" alt="{{ photo.title }}">

</a>

{% endfor %}
{% endblock content %}


views.py
def about(request):
gallery = Gallery.objects.all().is_public()
context = {'gallery': gallery}
return render(request, 'blog/gallerytest.html', context)
Message has been deleted

Ross Symonds

unread,
Jun 10, 2020, 9:45:30 AM6/10/20
to Django Photologue


On Wednesday, 10 June 2020 02:34:48 UTC, Hernan wrote:
Looks like you are iterating galleries not a gallery.

galleries = Gallery.objects.all().is_public()

{% for gallery in galleries %}

{% for photo in gallery %}


views.py 
I ended up using this code to just show photos from gallery 1. I think the 1 represents the galleries primary key. 


class about(request, ListView):
#gallerychoose = Gallery.objects.all().filter(title='Ha Giang Day 3')
gallery = Photo.objects.all().filter(galleries='1')
context = {'gallery': gallery}
return render(request, 'blog/gallerytest.html', context) 


I think changed it to a class based view because I wanted pagination.

class about(ListView):
    model = Photo
    template_name = 'blog/gallerytest.html'  # <app>/<model>_<viewtype>.html
    context_object_name = 'gallery'
    paginate_by = 4

    def get_queryset(self):
      #user=get_object_or_404(User,username=self.kwargs.get('username'))
      return Photo.objects.filter(galleries='1')


galleytest.html

{% extends "blog/base.html" %}
{% block content %}
<h2>Gallery Testing</h2>



{% for photo in gallery %}



<a href="{{ photo.image.url }}">
    <img src="{{ photo.get_thumbnail_url }}" alt="{{ photo.title }}">
</a>


{% endfor %}

{% if is_paginated %}
 <nav aria-label="Page navigation conatiner">
    <ul class="pagination justify-content-center">

    {% if page_obj.has_previous %}
      <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
    
    {% endif %}

    {% for num in page_obj.paginator.page_range %}
      {% if page_obj.number == num %}
      <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
      {% elif num > page_obj.number|add:'-2' and num < page_obj.number|add:'2' %}
      <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
      {% endif %}
     
  {% endfor %}

   {% if page_obj.has_next %}
      
      <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
    {% endif %}
</ul>
  </nav>
{% endif %}



{% endblock content %}



On Jun 9, 2020, at 10:09 PM, Ross Symonds <ross.kie...@gmail.com> wrote:

I am now being shown my three galleries. Instead of the contents of the galleries. 

<Auto Generated Inline Image 1.png>
-- 
You received this message because you are subscribed to the Google Groups "Django Photologue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-photologue+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-photologue/f0f25212-1954-48c9-9d84-38ed5de75c44o%40googlegroups.com.
<Auto Generated Inline Image 1.png>

Richard Barran

unread,
Jun 10, 2020, 3:03:39 PM6/10/20
to django-p...@googlegroups.com
> #gallerychoose = Gallery.objects.all().filter(title='Ha Giang Day 3')
> gallery = Photo.objects.all().filter(galleries='1')
>

To get all the photos in a gallery try something like this:

photos = Photo.objects.filter(galleries__title='Ha Giang Day 3’)

Explanation: there’s a many-to-many relation between Gallery and Photo.
Photo.objects.filter(galleries_ etc… means you’re filtering against the attributes of any of the galleries linked to a photo.
Reply all
Reply to author
Forward
0 new messages