Y
Y
YanSao2018-08-30 19:10:59
Django
YanSao, 2018-08-30 19:10:59

How to add an image via the admin panel in Django?

I decided to make a site on the topic of fishing to gain experience and new knowledge. On this site I need to display articles with a picture. How many lessons reviewed! All the same, it does not display, only the alt displays the pictures, even the title with the text does not display. here is the manage.py code:

class Article(models.Model):
    title = models.CharField(max_length = 200)
    text = models.TextField()
    image = models.ImageField(upload_to='media/main_image_news', blank=True)
    date = models.DateTimeField()

I also added everything to settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

I added the following code to the urls.py of the project:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('mainPage.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

In the urls.py of the application package:
from django.views.generic import ListView,DetailView
from mainPage.models import Article
urlpatterns = [
  path('', ListView.as_view(queryset=Article.objects.all().order_by('-date')[:20], template_name='html/mainPage.html')),
]

Well, here is the HTML code of the page itself:
{% extends 'html/base.html' %}
{% load staticfiles %}
{% block title%}Главная{% endblock %}
{% block post %}

<h1>{{ Article.title }}</h1>
<img src="{{ Article.image.url }}" width='50' alt="ну блиииин, не работает">
<p>{{ Article.text }}</p>
<h3>{{ Article.date }}</h3>

{% endblock %}

Here's what happens after all the migrations:
5b8804eb69449007123714.png
5b8804df38381514048416.png
Please help! What a day I've been suffering!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question