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]
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?
I would really appreciate if you can help me to point out what I did wrong.
Thank you.