2017-04-12 187 views
1

我有問題顯示從Model.I圖像有標題而不是圖片。我不知道那是什麼原因。圖片無法顯示Django

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> 

我認爲這將是與正確的軌道,以圖片的問題。我加入數據庫和圖片上傳圖片文件夾

回答

0

您需要指定模板

<img src="{{obj.image.url}}" />. 

你是存儲在數據庫中上傳路徑,所以在src你可以有OBJ。圖像輸入,應該能夠看到圖像就好。

+0

。這有效,但我不能設置寬度和高度圖像沒有顯示 – Gornl