1

I'm having difficulties in displaying the images that I upload from admin page for each question. In the admin page of Question, I upload image for each question and it's saved in correct directory. However, in the public view, I can't display these images for each question (each question will have a different image based on what I uploaded). [example attached]

enter image description here

Below are the code:

models.py

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    image_movie = models.ImageField(upload_to='static/polls/images', default='static/polls/images')

views.py

def image(request):
    images = Question()
    var = RequestContext(request,{
        'images':images
    })
    return render_to_response('detail.html', var)

detail.html

<form action="{% url 'polls:vote' question.id %}" method="post">{% csrf_token %}

<img src="{{ question.image_movie.url }}" />

{% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

polls/urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/static/polls/images/', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

I tried to find solutions in other places as well but I don't get it and can't apply to my problem. They are: Django - How can I display a photo saved in ImageField?

Django - Display ImageField

I would really appreciate if you can help me to point out what I did wrong.

Thank you.

Community
  • 1
  • 1
ZCox
  • 89
  • 11
  • Is this with debug set to true or false? ([Reason for asking](http://stackoverflow.com/a/7639983/1324033)) – Sayse Jul 22 '15 at 20:50
  • The most likely cause of the problem is that you either haven't configured the `MEDIA_URL` or `MEDIA_ROOT` correctly, or... you don't have a webserver to server your `MEDIA_ROOT` on the `MEDIA_URL` if not running the debug server. – Wolph Jul 22 '15 at 21:00
  • Thank you Sayse and Wolph. @Wolph: Can you give me recommended solution please? – ZCox Jul 22 '15 at 21:04
  • @ZakkCox: can you tell us how you are running Django right now? `runserver`?, Gunicorn?, Uwsgi? – Wolph Jul 22 '15 at 21:05
  • @Wolph: Yes, runserver. – ZCox Jul 22 '15 at 21:08
  • try using `runserver --insecure` – Sayse Jul 22 '15 at 21:25
  • @Sayse: It doesn't work as well :(. But when I "inspect element", there is an error which is "Failed to load resource: the server responded with a status of 404 (NOT FOUND) -http://127.0.0.1:8000/polls/2/static/polls/images/batman.jpg" – ZCox Jul 22 '15 at 21:54
  • @Sayse: I fixed the above error by changing the urls (adding /static/polls/images/) but the images haven't been displayed. – ZCox Jul 22 '15 at 22:09
  • @Wolph, Sayse: Thank you mates. I finally fixed my problem based on STATIC_URL. – ZCox Jul 23 '15 at 15:12
  • Good to hear :) Please add your solution as an answer as well for others with the same problem :) – Wolph Jul 23 '15 at 15:13

1 Answers1

1

I fixed this problem by changing the code of detail.html to:

{% load staticfiles %}
    <img src="{% static question.image_movie.url %}" style="width:500px;height:320px"/>

It means the images will be saved and loaded in /static/polls/images. (For example: "127.0.0.1/static/polls/images" instead of "127.0.0.1/polls/1/static/polls/images" as before)

The system seems to not recognise "question.id" to display the images from the directory.

ZCox
  • 89
  • 11