简体   繁体   English

如何连接Django 4中的图片?

[英]How to connect image in Django 4?

I have a project, connected database, one picture and form for send another picture.我有一个项目、连接的数据库、一张图片和用于发送另一张图片的表格。 In home page i want connect all pictures from DB but i don't understand how it's work.Please help me, because i try all what i found in inte.net在主页上,我想连接来自 DB 的所有图片,但我不明白它是如何工作的。请帮助我,因为我尝试了我在 inte.net 中找到的所有内容

settings.py设置.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

urls.py网址.py

from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    path("", views.products),
    path("sell/", views.sell),
    path("created/", views.created),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)
else:
    urlpatterns += staticfiles_urlpatterns()

models.py模型.py

from django.db import models

class Product(models.Model):
    ...
    product_media = models.ImageField(upload_to="images", verbose_name="Фотографії товару")

views.py视图.py

from django.shortcuts import render
from .forms import SellForm
from .models import Product
from main.settings import MEDIA_URL, MEDIA_ROOT

def products(request):
    product_list = Product.objects.all()
    return render(request, "products.html", {"product_list": product_list, "media_url": MEDIA_URL, "media_root": MEDIA_ROOT})

products.html产品.html

<!DOCTYPE html>
<html lang="uk">
<head>
  <style>
   ...
  </style>
</head>
<body>

<h1>Оголошення </h1><a href="sell/">Створити нове оголошення</a>
{% if product_list %}
<table border="1">
    <tr>
        <th>Id</th>
        <th>Час створення</th>
        <th>Профіль продавця</th>
        <th>Ім'я продавця</th>
        <th>Прізвище продавця</th>
        <th>Телефон продавця</th>
        <th>Електронна пошта продавця</th>
        <th>Назва оголошення</th>
        <th>Опис</th>
        <th>Медіа</th>
    </tr>
    {% for object in product_list %}
    <tr>
        <td>{{ object.id }}</td>
        <td>{{ object.product_dt }}</td>
        <td>{{ object.product_username }}</td>
        <td>{{ object.product_name }}</td>
        <td>{{ object.product_lastname }}</td>
        <td>{{ object.product_phone }}</td>
        <td>{{ object.product_email }}</td>
        <td>{{ object.product_productname }}</td>
        <td>{{ object.product_description }}</td>
        <td><img src="{{object.product_media.url }}" alt="connect" /></td>   <!-- I DONT UNDERSTAND HERE -->
    </tr>
    {% endfor %}
</table>
{% else %}
Таблиця наразі пуста. Заповніть заявку вище.
{% endif %}
</body>
</html>

PS: I'm newbie in Django, and it's my first project, i learning Django with it and a only want know how to connect images what i send to page from sqlite3 PS:我是 Django 的新手,这是我的第一个项目,我用它学习 Django 并且只想知道如何连接我从 sqlite3 发送到页面的图像

Project tree:项目树:

├──application
│  ├──accounts
│  │  ├──__pycache__
│  │  ├──migrations
│  │  │  └──__pycache__
│  │  └──templates
│  ├──main
│  │  └──__pycache__
│  ├──media
│  │  └──images
│  ├──products
│  │  ├──__pycache__
│  │  ├──migrations
│  │  │  └──__pycache__
│  │  └──templates
│  ├──templates
│  │  └──registration
│  └──uploads

Solved:解决了:

Just need write + urlpatterns in main urls.py and {{ object.product_media.url }}只需要在主 urls.py 和 {{ object.product_media.url }} 中写入 + urlpatterns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM