S
S
Sergey Nizhny Novgorod2015-12-29 06:25:43
Django
Sergey Nizhny Novgorod, 2015-12-29 06:25:43

How to use an ImageField in Django?

Hello.
Task: upload a photo through the junga admin panel and upload it to the site template.
What I do: # The view contains a couple of parts from another model, but in general this does not interfere
1) Media roots and statics

STATIC_URL = '/static/'

MEDIA_ROOT = ''

MEDIA_URL = ''

2) Model:
class Step(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    annotation = models.TextField()
    main_text = models.TextField()
    main_photo = models.ImageField(upload_to='bakot/static/bakot/imagination', height_field=None, width_field=None, max_length=100)
    true_question = models.TextField()
    true_answer = models.TextField()

3) View
def step1(request):
    stepfields = Step.objects.get()
    navigators = Navigation_bar.objects.all()
    context = {
        "stepfieldst" : stepfields,
        "navigators" : navigators,
    }
    return render_to_response('bakot/step1.html', context)

4) Pattern:
<div class="start-imagination">
    <img src="{%  block photocontent %}{% endblock %}">
</div> <!--Изображение -->

---

{% block photocontent %}{{ stepfieldst.main_photo.url }}{% endblock %}

5) As a result, there is no picture in the template:
- The link is inserted into the template
- The link looks like: 127.0.0.1:8000/admin/bakot/step/1/bakot/static/bak...
- When copying the link, the following error occurs:
155c17ab1a1d4ccaa8b87cd8d38c0319.jpg
I watched videos of other guys on youtube, they seem to do the same as me.
____________________
I found my own problem, which was fixed, but for the new version of Junga, it no longer wants to work:
Bourgeois problem from 2011,
i.e. determined media roots, added an upload to the model, uploaded a photo = received a message that the image is not found at this url.
the code that is given there as a remedy does not work on version 1.9 on the local machine.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
baterson, 2015-12-29
@Terras

Is the path to the statics specified in settings.py?
Usually this

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

And the media settings are the same
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

and add the following entry in the urls
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [....
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Maybe you wanted to take all the objects? Step.objects.all()otherwise it is not clear how you select a specific object
. If you take them all, then in the template you can display a photo through a cycle
{% for img in stepfieldst %}
<img src="{{ img.main_photo.url }}" >
{% endfor %}

If you select by a specific object, then the code will be the same, just not in a loop

S
Sergey Nizhny Novgorod, 2015-12-29
@Terras

I found my own problem, which was fixed, but for the new version of django, it no longer wants to work:
stackoverflow.com/questions/5517950/django-media-u...
i.e. determined media roots, added an upload to the model, uploaded a photo = received a message that the image is not found at this url.
the code that is given there as a remedy does not work on version 1.9 on the local machine.

N
nikelen, 2015-01-02
@nikelen

I saw a detailed video analysis of installing and displaying images on a local server: https://www.youtube.com/watch?v=3iVPjZgWkHg . Reason 1- correct urls (matches)
2- correct paths for pictures (on local server)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question