我正在尝试将图片添加到我的Django网络应用程序中。我的models.py中有一个模型,它看起来像这样:
# 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我有以下内容:
# 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函数渲染我的视图:
# 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)
但是当我尝试通过输入以下内容获取图片网址时,在我的模板上
<ul class="slides">
{% for image in featured_images %}
<li>
<div class="banner-bottom" style="background: url( {{ image.Imagem.url }} )no-repeat center;">
<div class="container">
<div class="bann-text">
{% if lang == 'pt' %}
<h3> {{image.Titulo}} </h3>
<p>{{image.Texto}}</p>
<p>{{image.Imagem}}</p>
{% else %}
<h3> {{image.Title}} </h3>
<p>{{image.Text}}</p>
{% endif %}
</div>
</div>
</div>
</li>
{% endfor %}
</ul>
我的标题显示但图像没有显示。当我检查页面的源代码时,我将图像标记作为:
<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
拉丁虚拟代码是我模型中其他东西的一部分
答案 0 :(得分: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'