我有一点问题,我不知道为什么我的项目不能按我的意愿工作。我有一个 models.py
class Strona(models.Model):
title = models.CharField(max_length=250, verbose_name="Tytuł", help_text="Podaj tytuł artykułu")
slug = models.SlugField(unique=True, verbose_name="Adres url SEO", help_text="Automatycznie tworzony przyjazny adres URL")
content = HTMLField(verbose_name="Treść artykułu", help_text="Wypełnij treścią artykułu")
image = models.FileField(upload_to='blog_images',verbose_name="Obrazek", help_text="Załącz obraz")
并在 view.py 我有
from django.shortcuts import render
from .models import Strona
def strona_glowna(request):
strona_glowna = Strona.objects.all()
context = {'strona_glowna': strona_glowna}
return render(request, 'strona/index.html', context=context)
之后我创建了html文件并包含在代码中:
...
{% for strona in strona_glowna %}
<strong>{{strona.title}}</strong><br>
{% if strona.image == True %}
<img src="{{strona.image.url}}"><br>
{% else %}
{% endif %}
<p>{{strona.content|safe}}<p></p>
{% endfor %}
...
在 setting.py 中声明:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
问题是,当我重新加载网站时,图像没有显示。我认为上传的路由是可以的,但我不知道为什么我无法显示图像。有什么想法吗?
更新
编辑:
当我在myapp / urls.py中添加代码时:
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from . import views
urlpatterns = [
path('', views.strona_glowna, name="strona_glowna"),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_ROOT, document_root=settings.MEDIA_ROOT)
并在setting.py中设置代码:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
ENV_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(ENV_PATH, '/static/')
MEDIA_ROOT = os.path.join(ENV_PATH, '/media/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
django回答了我一个错误:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/media/images/allmini.jpg
Using the URLconf defined in moja.urls, Django tried these URL patterns, in this order:
admin/
[name='strona_glowna']
^static\/(?P<path>.*)$
^D\:\/media\/(?P<path>.*)$
tinymce/
The current path, media/images/allmini.jpg, didn't match any of these.
Mayby你知道bug在哪里 - 我找不到它...... 在Google Chrome检查器中,路径为:
<img src="images/allmini.jpg">
答案 0 :(得分:0)
如果您没有{% load static %}
将其添加到您的html文件之上,那么在您的settings.py中添加MEDIA_ROOT
以及媒体文件夹的完整路径,例如是我的MEDIA_ROOT
:
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images/')
这是我的MEDIA_URL
:
MEDIA_URL = '/'
编辑:
我模特中的字段:
profile_picture = models.ImageField(
upload_to=get_user_upload_path, default=os.path.join(settings.MEDIA_ROOT, "profile_picture.png"),
blank=True, null=True
)
将图像上传到字段的方法:
def get_user_upload_path(instance, filename):
""" Crea il percorso ed il nome del file per l'upload. """
# Recupero l'estensione dell'immagine.
extension = filename.split('.')[-1]
filename_no_ext = "%s_%s_%s_%s" % ('img', instance.first_name, instance.last_name, instance.pk)
filename = "%s_%s_%s_%s.%s" % ('img', instance.first_name, instance.last_name, instance.pk, extension)
# Elimino la vecchia immagine del profilo.
try:
os.remove(os.path.join(settings.MEDIA_ROOT, "users/", "%s.%s" % (filename_no_ext, "jpg")))
except OSError:
pass
try:
os.remove(os.path.join(settings.MEDIA_ROOT, "users/", "%s.%s" % (filename_no_ext, "jpeg")))
except OSError:
pass
try:
os.remove(os.path.join(settings.MEDIA_ROOT, "users/", "%s.%s" % (filename_no_ext, "png")))
except OSError:
pass
# Ritorno il percorso completo del file.
return os.path.join(settings.MEDIA_ROOT, "users/", filename)
像这样,用户个人资料图片将保存在projectname/static/images/users/filename
编辑:
如果您没有,请将此添加到您的urls.py:
urlpatterns = [
...
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_ROOT, document_root=settings.MEDIA_ROOT)
答案 1 :(得分:0)
要使用模板访问图像:
{{strona.image}}。 strona.image没有名为url
的密钥。
注意:strona.image值将是一个URL本身。您可以通过在不同的标签中打印来检查它。