在django-templates/django-forms上显示图像/背景图像
问题描述:
我是Django的新手,我在显示图像的django HTML模板/表单时遇到问题。其实,我无法展示它们。我已经遵循这个计算器上提出的分辨率问题/链接 - How do I include image files in Django templates?在django-templates/django-forms上显示图像/背景图像
但是,当我在我的浏览器上每次测试时仍然会看到破损的图像时。 下面是有关我的应用程序文件(可以有很大的帮助精确定位什么是错的)的部分内容:
--- settings.py ---
# Media and images used by the templates
MEDIA_ROOT = '/home/acresearch/djangoProjects/fringe/fringe/images'
MEDIA_URL = 'http://localhost:8000/images/'
---网址。 PY ---
urlpatterns = patterns('',
(r'^submit/$', submit),
(r'^receipt/$', receipt),
(r'^images/(?P<path>.*)$', 'django.views.static.serve', {'document_root' : settings.MEDIA_ROOT}),
--- --- views.html
def submit(request):
if request.method == 'POST':
form = SubmitForm(request.POST, request.FILES)
if form.is_valid():
<omitted code related to handling file upload>
else:
form = SubmitForm()
return render_to_response('submission_form.html', {'form' : form})
--- --- forms.py
class SubmitForm(forms.Form):
email = forms.EmailField(required=True, label='Email address:')
uploadedfile = forms.FileField(required=True, label='File to upload:')
--- submission_form.html ---
<body bgcolor="gray" >
<h1>GRID PE-Classifier Submission Portal</h1>
{% if form.errors %}
<p style="color: red;">Please correct the error{{ form.errors|pluralize }} below. </p>
{% endif %}
<img src="{{ MEDIA_URL }}GRID_LOGO_rev4.jpg" />
<form enctype="multipart/form-data" action="" method="post" >
<div class="field">
<table>
{{ form.email.label }}
{{ form.email }}
</table>
<em>{{ form.email.errors }}</em>
</div>
<div class="field">
<table>
{{ form.uploadedfile.label }}
{{ form.uploadedfile }}
</table>
<em>{{ form.uploadedfile.errors }}</em>
</div>
<input type="submit" value="Submit">
</form>
</body>
最后一件事,我检查了开发服务器的响应,并看到了,我应该作为显示的图像的JPG我HTML模板/表单作为GET参数传递给views.py的提交功能。见下:
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[24/Apr/2012 04:55:57] "GET /submit/ HTTP/1.1" 200 621
[24/Apr/2012 04:55:57] "GET /submit/GRID_LOGO_rev4.jpg HTTP/1.1" 404 2208
希望你能帮助我在这(因为这是我的项目的最后一站,我很卡住)。另外,如果你能指向我一些综合的,但直接指向关于在Django上使用CSS和JS来美化你的网页的点引用,那也会很棒。
在此先感谢!
答
您还没有使用RequestContext
来呈现您的模板,因此MEDIA_URL
未被传递。
谢谢丹尼尔......但它没有工作:(我将views.py中的提交函数中的行代替为使用RequestContext的东西。 return render_to_response('submission_form.html',{'form':form },context_instance = RequestContext(request)) – jaysonpryde 2012-04-24 11:03:53
有没有更多想法? – jaysonpryde 2012-04-24 16:25:04
您是否已将“django.core.context_processors.media”添加到settings.py中的TEMPLATE_CONTEXT_PROCESSORS? – dannyroa 2012-04-24 19:36:59