我想在django模板中显示上载的图像,但未显示任何图像,我已成功上载并保存了它,我该如何解决此问题,请任何人请解释一下或我们提供一个示例演示配置我们的settings.py以在模板中显示图像?
我的settings.py:
LOGIN_URL='/login'
STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/home'
LOGOUT_REDIRECT_URL='/accounts'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
我的模型。py:
class Images(models.Model):
user_id=models.ForeignKey(User,on_delete=models.CASCADE,null=True)
image=models.FileField(upload_to='home')
我用来显示它的图像标签是
<img src="{{ obj.image.url }}" class="img-circle" height="65" width="65" alt="Avatar">
我的urls.py:
from django.urls import path
from . import views
from . models import FavBooks
from django.conf import settings
from django.conf.urls.static import static
app_name='home'
urlpatterns=[
path('',views.HomeView,name='home'),
path('addBooks/',views.addBooks,name='addBooks'),
path('uploadpic/',views.uploadPic,name='uploadPic'),
path('myBooks/',views.BooksView.as_view(),name='myBooks'),
path('<int:pk>/', views.BookDetailsView.as_view(), name='myBooks'),
path('search/', views.SearchedBooks, name='searchedBooks'),
path('favoriteAjax/', views.favorite_ajax, name='favoriteAjax'),
path('removefavoriteAjax/', views.removefavorite_ajax, name='removefavoriteAjax'),
path('wishList/',views.WishlistView.as_view(),name='wishList'),
path('deletebook/', views.deleteBook, name='deleteBook'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 0 :(得分:0)
将此添加到urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)