Sir i am working on the django tweeter like project

13 views
Skip to first unread message

niranjan shukla

unread,
Jul 19, 2019, 10:33:00 AM7/19/19
to Django users
But Problem is I don't get the images 

model.py
----------------------


class tweet_upload(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length = 100)
    text = models.CharField(max_length=300)
    created_date = models.DateTimeField(auto_now_add=True)
    uploaded_image = models.ImageField(default = 'default.jpg', upload_to = 'static')

    def __str__(self):
        return f'{self.user.username} tweet_upload'

    def save(self,*args, **kwargs):
        super().save(*args, **kwargs)

        img =  Image.open(self.uploaded_image.path)
        if img.height >300 or img.width>300:
            output_size = (300,300)
            img.thumbnail(output_size)
            img.save(self.uploaded_image.path)


urls.py
----------------

from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'$',views.post_view, name = "view"),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)



settings.py
---------------------



MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'static')





forms.py
------------------

from django import forms
from .models import tweet_upload
class tweetform(forms.ModelForm):
    class Meta:
        model = tweet_upload
        fields = ['uploaded_image','text','created_date','user','title']




admin.py
---------------------

from .models import tweet_upload
admin.site.register(tweet_upload) 



views.py
--------------

from testapp.models import tweet_upload
from django.db.models import Q
# Create your views here.
def post_view(request):
    tweet_list = tweet_upload.objects.all()
    a = tweet_upload.objects.get(title = "Medicine (Django)").uploaded_image
    b = a.url
    return render(request,'post_view.html', {'tweet_list': tweet_list,'b':b})


post_view.html
_______________


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>tweet</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </head>
  <body>
    <div class="container" style="margin : 0px; padding : 0px;">
    {% if tweet_list%}
    {%if b%}
    {% for i in tweet_list %}
      <li>{{i.user}}</li>
      <li>{{i.title}}</li>
      <li>{{i.text}}</li>
      <li>{{b}}</li>      {%endfor%}

      <img src="{{b}}" alt="not found">
      {%else%}
      <p>Niranjan is fell sorry for that </p>
      {%endif%}
      {%endif%}


    </div>

  </body>
</html>






2019-07-19.png



Explain why I don't I get the image

Irfan Khan

unread,
Jul 20, 2019, 2:53:16 AM7/20/19
to django...@googlegroups.com


Write there <img arc=“{{b.image.url}}” > 
Add image.url 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d171eefd-0afd-4273-ac64-a7d325472056%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages