Home > Blockchain >  Getting a TypeError while trying to make a unique page in Django
Getting a TypeError while trying to make a unique page in Django

Time:09-03

I'm making a picture gallery web-app. I want to make some of displayed photos to belong to a specific collection. Each collection is supposed to have its own page that displays all of the pictures that belong to it.

The name of each unique page is supposed to be photo_collection model, which I added to the class Image in models.py. But for some reason, I get TypeError at /projects/sample_collection_name/ - photo_collection() got an unexpected keyword argument 'photo_collection'

No idea what's causing this error. I tried renaming the photo_collection function (it has the same name as my model), but it didn't work.

models.py

class Image(models.Model):
  title = models.CharField(max_length=300)
  image = models.ImageField(null=True, blank=True, upload_to='images/')
  pub_date = models.DateTimeField('Date published', default=timezone.now)
  photo_collection = models.CharField('Photo collection', max_length=250, null=True, blank=True)

views.py

def photo_collection(request):
  image = Image.objects.all()
  return render (request, 'photo_app/collection.html', context={'image': image})

urls.py

urlpatterns = [
  #some other patterns here
  path('projects/<str:photo_collection>/', views.photo_collection, name='photo_collection'),
]

gallery.html

        {% if i.photo_collection != Null %}
           <a href="{% url 'photo_collection' i.photo_collection %}">{{i.photo_collection}}</a>
        {% endif %}

CodePudding user response:

you need to add photo_collection to your view parameters.

it will be like this:

def photo_collection(request, photo_collection):
  image = Image.objects.all()
  return render (request, 'photo_app/collection.html', context={'image': image})

when ever you add a variable to your url path you need to add that variable to view parameters too.

here is the documentation for this matter: https://docs.djangoproject.com/en/4.1/topics/http/urls/

  • Related