我正在使用 inlineformset 以便用户可以一次上传多个图像。图像已保存,功能如预期,但前端除外。当我使用类似于 {{ form. image }},我可以清楚地看到我的图像已保存,当我单击 url 时,我被重定向到上传的文件。问题似乎是当我尝试将图像的 URL 设置为图像元素的 src 时,绝对 url 没有被存储。
尝试在<p>标签中记录 MEDIA_URL 和 MEDIA_ROOT不会产生任何结果。
设置.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
ROOT_URLCONF = 'dashboard_app.urls'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
网址.py
from django.conf.urls import url, include
from . import views
from django.conf.urls.static import static
from django.conf import settings
app_name = 'Accounts_Namespace'
urlpatterns = [
url(r'^$', views.Register, name='Accounts_Register'),
url(r'^change-password/$', views.ChangePassword, name="Accounts_Change_Password"),
url(r'^login/$', views.Login, name='Accounts_Login'),
url(r'^logout/$', views.Logout, name='Accounts_Logout'),
url(r'^profile/$', views.ViewProfile, name='Accounts_View_Profile'),
url(r'^profile/edit/$', views.EditProfile, name="Accounts_Edit_Profile"),
url(r'^school/', include('student_map_app.urls', namespace="Student_Maps_Namespace")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
模型.py
class Gallery(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
image = models.ImageField(upload_to="gallery_images")
uploaded = models.DateTimeField(auto_now_add=True)
再次,当我尝试:
<img src="{{ MEDIA_URL }}{{ gallery_form.image.url }}">
我得到了“未知”的值作为来源,但我可以单击“{{ gallery_form.image}}”生成的链接并查看上传的图像。尝试同时记录“MEDIA_URL”和“MEDIA_ROOT”不会产生任何结果。不太确定问题出在哪里。
相关分类