图片无法显示Django

时间:2017-04-12 18:26:13

标签: python django

我的问题与显示来自Model的图像有关。我有标题而不是图片。我不知道是什么原因造成的。

 from django.db import models

class Games(models.Model):
    name = models.CharField(max_length=250)
    platform = models.CharField(max_length=500)
    genre = models.CharField(max_length=100)
    language = models.CharField(max_length=100)
    image = models.ImageField(upload_to='Images_game',default='' )
  # def __str__ (self):
   # return self.Name

class Game_detail(models.Model):
    game = models.ForeignKey(Games,on_delete=models.CASCADE)
    subcryption = models.TextField()
    publisher = models.CharField(max_length=250)
    date_of_publish = models.DateField()

class Order(models.Model):
    person_name = models.CharField(max_length=24)
    person_surname = models.CharField(max_length=24)
    street = models.CharField(max_length=45)
    city= models.CharField(max_length=45)

views.py

from django.shortcuts import render
    from .models import Games,Game_detail,Order
    from django.shortcuts import render,get_object_or_404,redirect
    from django.views.generic import ListView,DetailView,CreateView

        class IndexView(ListView):
            template_name ='games/list.html'
            context_object_name = 'all_games'
            model = Games
            def get_queryset(self):
                return Games.objects.all()


        class Item_Detail (DetailView):

            context_object_name = 'object'
            template_name ='games/detail.html'
            model = Games # tu jest wazna zmiana

            def get_context_data(self, **kwargs):
                context = super(DetailView,self).get_context_data(**kwargs)
                context['Game_detail']=Game_detail.objects.all()
                return context



        class Create_order (CreateView):
            template_name ='games/create.html'
            model= Order
            fields = ['person_name','person_surname','street','city']

settings.py

from django.conf.urls import include,url
from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
     url(r'^admin/', admin.site.urls),
     url(r'^games/',include("games.urls")),
   ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

list.html模板

<!DOCTYPE html>
<html>
<head>
 <title>Page Title</title>
 </head>
 <body>
{% for obj in all_games%}
<a href ='{%url "detail" pk=obj.pk%}'>{{obj.name}}</a>
{{obj.image}}
{{obj.platform}}
{{obj.genre}}
{{obj.language}}
{% endfor %}


</body>
</html>

我认为跟踪图片是正确的问题。我将图片添加到数据库并将图片上传到文件夹

1 个答案:

答案 0 :(得分:0)

您需要在模板中指定

Sub SaveWorksheetsAsCsv()

Dim WS As Excel.Worksheet
Dim SaveToDirectory As String

Dim CurrentWorkbook As String
Dim CurrentFormat As Long

CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook

'Change the path, must end with \
SaveToDirectory = "C:\Users\username\Documents\test\"
For Each WS In ThisWorkbook.Worksheets
    Sheets(WS.Name).Copy
    ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV
    ActiveWorkbook.Close savechanges:=False
    ThisWorkbook.Activate
Next
'edit/remove the for loop to suit
End Sub

您正在将上传路径存储在数据库中,因此在src中您可以输入obj.image,并且应该能够正常查看图像。