Forums

How to put different pictures into different poll pages - ie. polls/1, polls/2 etc.?

I'm trying to get different pictures into each polls page , but how to do this?

so far I have the same pictures for each question by putting in an image link in polls/index.html, detail, results

how to do? thanks

If you have a django webapp, perhaps use django templating and pass in a different image url from your views?

Where to put the image url though?

Do you mean instert a link to it in polls/detail.html?

Yes -- if each Poll object has an image URL associated with it, then you can create an appropriate <img> tag inside the template that sets the src attribute to that URL.

but where to put the code for the actual image?

for example: politics.jpg needs to end on the page polls/2,
sport.jpg needs to end up on page polls/3 etc.

how to get this to happen? thanks

If each page is rendering an object, then the object needs to contain the URL of the image. It might be worth going through a Django tutorial (or, if this question is coming about because you're already going through one, a different Django tutorial) because it's likely to all be explained there better than I can in these forums. I strongly recommend the "Django Girls" one -- it was originally written to introduce young women to programming, but it's an excellent tutorial for anyone of any age or gender.

How to set up the django admin so that you can change the images there - ie. an image for each question?

If you define the image on the object using a Django ImageField then it will have a file upload option on the admin.

ok, thanks, I'm working on that now

I've added this command: image = models.FileField(upload_to='post_image', blank=True), to models.py (and numerous combinations) then run makemigratisons, migrate etc..from bash terminal - but nothing appears on djangoadmin site re. file browser for images etc..

what am I missing here, do I need to add some settings to admin.py?

Did you reload your web app after making the code change?

yes done that, makes no difference

how about this part: (upload_to='post_image', blank=True)

is that correct? I mean should it upload to 'post' - where does it upload from and to etc? I feel I may have something wrong here

I suspect not. That's a path. It's not a good idea to use relative paths. See http://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

I've tried this: (upload_to='static/polls/images/picture.jpg') and (upload_to'http://blueforest/static/polls/images') and various combinations ,but no success

how does it know where to upload from etc., and what next to try.?

Your best option here is to read and understand the documentation of the FileField class in Django. Here's a link to the docs for Django 2.0; if you're using a different version, you can select it from the menu at the bottom right of the page. The most important thing to understand is how it interacts with the MEDIA_ROOT that you set in your settings.py.

ok, I'll check that out, thanks

ok, so I;ve got the upload function on django admin working - set media roots etc.. yet the image still does not show on the web app page

for example, in details.html I have this code

<form action="{% url 'polls:vote' question.id %}" method="post"> {% csrf_token %} {% 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 />

<img src="{{ question.GreenParty_knlKu95.jpg}}"/>

the last line does not seem to show the image on the polls quesiton page - is this in the wrong place?

models.py has this:

class Question(models.Model): question_text = models.CharField(max_length=200)

image = models.FileField(upload_to='uploads/', blank=True)

pub_date = models.DateTimeField('date published')

no worries: I've added this : <img src="{{ question.image.url }}" alt="I will work" />

at the end, seems to be working now