当我尝试从媒体根目录提供它时,我得到了这个损坏的图像。恳请您..不要向我显示文档或以前问题的链接。我已经尝试过这些东西,但我仍然得到这个破碎的图像。
模型.py:
class BasicModel(models.Model):
name = models.CharField(max_length=200)
dob = models.DateField()
photo = models.ImageField(upload_to='sample')
class BasicModelForm(ModelForm):
class Meta:
model = BasicModel
视图.py:
def BasicView(request):
if request.method == 'POST':
form = BasicModelForm(request.POST, request.FILES)
if form.is_valid():
data = form.save()
return preview(request, data.id)
else:
form = BasicModelForm()
return render_to_response("unnamed.html", {'form': form}, context_instance=RequestContext(request))
def preview(request, id):
obj = get_object_or_404(BasicModel, pk=id)
return render_to_response("preview.html", {'obj': obj})
设置.py:
MEDIA_ROOT = '/home/nirmal/try/files/'
MEDIA_URL = 'http://localhost:8000/files/'
网址.py:
url(r'^unnamed/$', 'unnamed.views.BasicView'),
url(r'^files/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
预览.html:
<html>
<body>
{{ obj.name }}
{{ obj.dob }}
<img src="{{ MEDIA_URL }}sample/{{ obj.photo }}" />
</body>
</html>
有人可以帮我吗?
谢谢!