0

enter image description hereGeting error ValueError at / The 'image' attribute has no file associated with it.

when I try to post a image from a user not having admin access then i am getting this error and the picture doesnot display.

index.html

 <div class="container m-auto">

                <!--<h1 class="lg:text-2xl text-lg font-extrabold leading-none text-gray-900 tracking-tight mb-5"> Feed </h1>--->

                <div class="lg:flex justify-center lg:space-x-10 lg:space-y-0 space-y-5">

                    <!-- left sidebar-->
                    <div class="space-y-5 flex-shrink-0 lg:w-7/12">

                        <!-- post 1-->

                        {% for post in posts reversed %}
                   
                        <div class="bg-white shadow rounded-md  -mx-2 lg:mx-0">
    
                            <!-- post header-->
                            <div class="flex justify-between items-center px-4 py-3">
                                <div class="flex flex-1 items-center space-x-4">
                                    <!-- <a href="#">
                                        <div class="bg-gradient-to-tr from-yellow-600 to-pink-600 p-0.5 rounded-full">  
                                            <img src="{% static 'assets/images/avatars/avatar-2.jpg' %}" class="bg-gray-200 border border-white rounded-full w-8 h-8">
                                        </div>
                                    </a> -->
                                    <span class="block capitalize font-semibold "> {{post.user}} </span>
                                </div>
                              <div>
                                <a href="#"> <i class="icon-feather-more-horizontal text-2xl hover:bg-gray-200 rounded-full p-2 transition -mr-1 "></i> </a>
                                <div class="bg-white w-56 shadow-md mx-auto p-2 mt-12 rounded-md text-gray-500 hidden text-base border border-gray-100  " uk-drop="mode: hover;pos: top-right">
                              
                                    <ul class="space-y-1">
                                      <!-- <li> 
                                          <a href="#" class="flex items-center px-3 py-2 hover:bg-gray-200 hover:text-gray-800 rounded-md ">
                                           <i class="uil-share-alt mr-1"></i> Share
                                          </a> 
                                      </li>
                                      <li> 
                                          <a href="#" class="flex items-center px-3 py-2 hover:bg-gray-200 hover:text-gray-800 rounded-md ">
                                           <i class="uil-edit-alt mr-1"></i>  Edit Post 
                                          </a> 
                                      </li>
                                      <li> 
                                          <a href="#" class="flex items-center px-3 py-2 hover:bg-gray-200 hover:text-gray-800 rounded-md ">
                                           <i class="uil-comment-slash mr-1"></i>   Disable comments
                                          </a> 
                                      </li> 
                                      <li> 
                                          <a href="#" class="flex items-center px-3 py-2 hover:bg-gray-200 hover:text-gray-800 rounded-md ">
                                           <i class="uil-favorite mr-1"></i>  Add favorites 
                                          </a> 
                                      </li>
                                      <li>
                                        <hr class="-mx-2 my-2 ">
                                      </li> -->
                                      <li> 
                                          <a href="#" class="flex items-center px-3 py-2 text-red-500 hover:bg-red-100 hover:text-red-500 rounded-md ">
                                           <i class="uil-trash-alt mr-1"></i>  Delete
                                          </a> 
                                      </li>
                                    </ul>
                                
                                </div>
                              </div>
                            </div>

                            <div uk-lightbox>
                                <a href="{{post.image.url}}">  
                                   <img src="{{post.image.url}}" alt="">
                                </a>
                            </div>
                         

** models.py**


class Post(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    user = models.CharField(max_length=100)
    image = models.ImageField(upload_to='post_images')
    caption = models.TextField()
    created_at = models.DateTimeField(default=datetime.now)
    no_of_likes = models.IntegerField(default=0)

    def __str__(self):
        return self.user

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('signup', views.signup, name='signup'),
    path('settings', views.settings, name='settings'),
    path('upload', views.upload, name='upload'),
    path('signin', views.signin, name='signin'),
    path('logout', views.logout, name='logout')
]

views.py


@login_required(login_url='signin')
def index(request):
    user_object = User.objects.get(username=request.user.username)
    user_profile = Profile.objects.get(user=user_object)

    posts = Post.objects.all()

    return render(request, 'index.html', {'user_profile': user_profile, 'posts': posts})

@login_required(login_url='signin')
def upload(request):

    if request.method == 'POST':
        user = request.user.username
        image = request.FILES.get('image_upload')
        caption = request.POST['caption']

        new_post = Post.objects.create(user=user, image=image, caption=caption)
        new_post.save()

        return redirect('/')
    else:
        return redirect('/')
    return HttpResponse('upload view')

i tried using {% if post.image%} then.....{% enfif %} in index.html then the error goes away but the post's picture doesnot come after this to

How can I correct this?

San07
  • 25
  • 4
  • please post the solution with the {% if... %} tag – Razenstein Apr 01 '23 at 10:32
  • I implemented the following but then the error goes away but the post picture isnt displaying {% if post.image %} `` {% endif %} @Razenstein – San07 Apr 01 '23 at 10:44
  • Can you check weather the image is uploaded to the server? – Sumithran Apr 01 '23 at 10:56
  • the posts are getting created on the server but without any pictures attached to it. the image section shows **no file chosen** @Sumithran – San07 Apr 01 '23 at 11:03
  • so the problem is in the upload(..). please check what you get as image there. the if tag looks ok – Razenstein Apr 01 '23 at 11:12
  • have you set [MEDIA](https://stackoverflow.com/questions/5517950/django-media-url-and-media-root) configurations in settings.py? – Sumithran Apr 01 '23 at 11:34

0 Answers0