Проблема с загрузкой и просмотром изображений с django - PullRequest
1 голос
/ 26 марта 2020

Надеюсь, кто-нибудь может мне помочь, спасибо! Я не могу понять, что не так с моим кодом. Я уже прочитал тонны статей об этой проблеме, но никто из них не помог мне. Это действительно просто, я пытаюсь загрузить динамическое c изображение с django, но у меня продолжает возникать эта проблема.

Атрибут 'cover' не имеет файла, связанного с ним.


    Environment:


    Request Method: GET
    Request URL: http://127.0.0.1:8000/band/

    Django Version: 3.0.4
    Python Version: 3.8.2
    Installed Applications:
    ['search.apps.SearchConfig',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles']
    Installed Middleware:
    ['django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware']


    Template error:
    In template C:\Users\arian\Dev\db_vinili\src\dbvinili\search\templates\search\band.html, error at line 22
       The 'cover' attribute has no file associated with it.
       12 :     <div class="cat">
       13 :       <h2> BAND </h2>
       14 :     </div>
       15 : 
       16 : 
       17 : {% for Info in object_list %}
       18 :   <div class="container">
       19 : 
       20 :     <div class=album>
       21 :       <!-- insert an image -->
       22 :       <img src= " {{Info.cover.url}} " width="100%">
       23 : 
       24 :     </div>
       25 : 
       26 : 
       27 :     <div class="info">
       28 :       <!-- insert table info -->
       29 :       <table>
       30 :         <tr>
       31 :           <td> Band </td>
       32 :           <td> {{Info.band}}</td>


    Traceback (most recent call last):
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 828, in _resolve_lookup
        current = current[bit]

    During handling of the above exception ('ImageFieldFile' object is not subscriptable), another exception occurred:
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
        response = get_response(request)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
        response = response.render()
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\response.py", line 105, in render
        self.content = self.rendered_content
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\response.py", line 83, in rendered_content
        return template.render(context, self._request)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\backends\django.py", line 61, in render
        return self.template.render(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 171, in render
        return self._render(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 163, in _render
        return self.nodelist.render(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 936, in render
        bit = node.render_annotated(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
        return self.render(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\defaulttags.py", line 209, in render
        nodelist.append(node.render_annotated(context))
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
        return self.render(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 986, in render
        output = self.filter_expression.resolve(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 670, in resolve
        obj = self.var.resolve(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 795, in resolve
        value = self._resolve_lookup(context)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 836, in _resolve_lookup
        current = getattr(current, bit)
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\fields\files.py", line 61, in url
        self._require_file()
      File "C:\Users\arian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\fields\files.py", line 38, in _require_file
        raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)

    Exception Type: ValueError at /band/
    Exception Value: The 'cover' attribute has no file associated with it.

Вид


    from django.shortcuts import render, redirect
    from django.http import HttpResponse

    from django.views.generic import TemplateView, ListView
    from django.views.generic.edit import CreateView
    from django.urls import reverse_lazy

    from django.contrib.auth import login, authenticate
    from django.contrib.auth.forms import UserCreationForm

    from .models import Info, Search

    class bandView(ListView):
        model = Info
        template_name='search/band.html'

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...