2017-01-17 63 views
0

我想将图片添加到我的Django网络应用程序。我在我的models.py一个模型,它看起来是这样的:ImageField url返回一个空字符串

# models.py 

from django.db import models 

# Create your models here. 
class Protese(models.Model): 
    '''Proteses usadas no High Five''' 

    # Parte independente da lingua 
    Nome = models.CharField(max_length = 200) 
    Autores = models.CharField(max_length = 200) 
    AutoresURL = models.CharField(max_length = 200) 
    Foto = models.ImageField() 
    Data = models.DateTimeField() 

    # Portugues 
    Descricao = models.TextField() 
    # Ingles 
    Description = models.TextField() 

    def __str__(self): 
     return self.Nome 

class Membro(models.Model): 
    '''Membros do projeto''' 

    # Parte independente da lingua 
    Nome = models.CharField(max_length = 200) 
    Foto = models.ImageField() 
    LinkedIn = models.CharField(max_length = 200) 
    # Portugues 
    Posicao = models.CharField(max_length = 200) 
    Frase = models.TextField() 
    # Ingles 
    Position = models.CharField(max_length = 200) 
    Quote = models.TextField() 

    def __str__(self): 
     return self.Nome 


class FeaturedImage(models.Model): 
    ''' Imagem que aparece no comeco da pagina principal''' 
    Image = models.FileField() 
    # Portugues 
    Titulo = models.CharField(max_length = 200) 
    Texto = models.TextField() 
    # Ingles 
    Title = models.CharField(max_length = 200) 
    Text = models.TextField() 

class Post(models.Model): 
    ''' Posts de noticias ''' 
    Imagem = models.ImageField() 
    # pt 
    Titulo = models.CharField(max_length = 200) 
    Texto = models.TextField() 
    # en 
    Title = models.CharField(max_length = 200) 
    Text = models.TextField() 

对于我的settings.py我有以下代码:

# settings.py 
# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.9/howto/static-files/ 

STATIC_URL = '/static/' 

STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static'])) 

MEDIA_ROOT = os.path.join(BASE_DIR,'media') 
MEDIA_URL = '/media/' 

和我的urls.py我有以下:

# views.py 
def index(request, lang): 
    context = { 
     'featured_images':FeaturedImage.objects.all(), 
     'proteses': Protese.objects.all(), 
     'posts': Post.objects.all(), 
     'lang': lang 
     } 
    return render(request, 'web/index.html',context) 

# urls.py 

from datetime import datetime 
from django.conf.urls import url 
import django.contrib.auth.views 

import app.models as models 
import app.views as views 

# Uncomment the next lines to enable the admin: 
from django.conf.urls import url,include 
from django.contrib import admin 
admin.autodiscover() 

from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    # Pagina principal 
    url(r'^$',views.index,kwargs = {'lang':'pt'} ,name = 'index'), 
    url(r'^index.html$',views.index,kwargs = {'lang':'pt'} ,name = 'index'), 
    url(r'^en/$',views.index,kwargs = {'lang':'en'} ,name = 'index'), 
    url(r'^en/index.html$',views.index,kwargs = {'lang':'en'} ,name = 'index'), 
    # Sobre nos 
    url(r'^about.html$',views.about, name = 'about'), 
    # Servicos 
    url(r'^services.html$',views.services, name = 'about'), 
    # Contato 
    url(r'^contact.html$',views.contact, name = 'about'), 
    # Proteses 
    url(r'^single/(?P<page_name>[A-Za-z0-9]+)/$',views.single, name='single'), 

    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    url(r'^admin/', include(admin.site.urls)), 
] 

if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT) 
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) 

我使用下面的views.py功能渲染我的看法

但后来在我的模板,当我尝试通过键入来获得图像URL:

​​

我有标题显示出来,但图像显示不出来。当我检查页面的源代码时,我有图像标签appering:

<div class="banner-bottom" style="background: url()no-repeat center;"> 

任何想法为什么发生这种情况? 下面是从数据库中输出,当我运行:

--dbshell 
select * from app_foo; 
1|Lorem Ipsum|At vero eos et accusamus et iusto odio dignissimos|Lore Ipsum|Sed ut perspiciatis unde omnis iste natus error|1.png 

凡拉丁伪代码部分操作系统在我的模型中的其他事情

+1

是否有foo的值设置为数据库中的图像? – 2ps

+0

是的,我创建了一个foo对象,我上传了一个图像,并试图显示它 –

+1

您可以请将值发布到数据库中吗? – 2ps

回答

1

更新:看到你的原始文件后,这看起来像一个更简单的问题。

在您的FeaturedImage型号上,您有一个Image字段,而不是Imagem字段。我不知道这是否是一个错字,但如果这是正确粘贴,这将是一个问题的来源。

即,你应该尝试:

<div class="banner-bottom" style="background: url({{ image.Image.url }})no-repeat center;"> 

Imagem是您Post模型场,而且可能已经混乱的根源。


你的观点看起来不可能的工作,所以有可能是一个问题,如果这是实际的视图文件:

# views.py 
def renderFoo(request): 
    # The below would give you a syntax error 
    context = { 
     'foos' = foo.objects.all(), 
     ''' other models here ''' 
    } 
    # You should be using: 
    context = { 
     'foos': foo.objects.all(), 
     # other models here 
    } 
    return render(request,'path/to/page.html',context)  

它应该正常工作。访问您所查看的url字段是the proper way to access the image URL

>>> car = Car.objects.get(name="57 Chevy") 
>>> car.photo 
<ImageFieldFile: chevy.jpg> 
>>> car.photo.name 
'cars/chevy.jpg' 
>>> car.photo.path 
'/media/cars/chevy.jpg' 
>>> car.photo.url 
'http://media.example.com/cars/chevy.jpg' 
+0

对不起,这是堆栈溢出错误,这不是实际的视图文件,在视图文件中是'foos':foo.objects.all()I将发布原始的,它可能会更容易看到错误。事情是,我可以参考我的模板上的其余内容只是不起作用的图像 –

+0

刚发布的原始文件有帮助吗? –