Python 在django模板/django表单上显示图像/背景图像
我是Django的新手,在Django HTML模板/表单上显示图像时遇到问题。实际上,我不能展示它们。我已经遵循了关于这个问题/链接的决议- 然而,当我每次测试浏览器时,仍然会在浏览器上看到一个损坏的图像。 以下是与我的应用程序相关的文件的部分内容(这对查明错误有很大帮助): ---设置.py--- ---url.py--- ---提交表格.html--- 希望你能在这方面帮助我(因为这是我项目的最后一站,我被卡住了)。另外,如果你能给我介绍一些关于在Django上使用CSS和JS来美化你的网页的全面但直接的参考资料,那也太好了Python 在django模板/django表单上显示图像/背景图像,python,django,templates,django-forms,Python,Django,Templates,Django Forms,我是Django的新手,在Django HTML模板/表单上显示图像时遇到问题。实际上,我不能展示它们。我已经遵循了关于这个问题/链接的决议- 然而,当我每次测试浏览器时,仍然会在浏览器上看到一个损坏的图像。 以下是与我的应用程序相关的文件的部分内容(这对查明错误有很大帮助): ---设置.py--- ---url.py--- ---提交表格.html--- 希望你能在这方面帮助我(因为这是我项目的最后一站,我被卡住了)。另外,如果你能给我介绍一些关于在Django上使用CSS和JS来美化你的网
提前谢谢 您没有使用
RequestContext
来呈现模板,因此没有传递媒体URL
。谢谢Daniel。。。但它不起作用:(.我将views.py中submit函数中的行替换为使用RequestContext的行。将render_返回到_response('submission_form.html',{'form':form},context_instance=RequestContext(request))您是否在settings.py中的模板_context_处理器中添加了“django.core.context_processors.media”?
# Media and images used by the templates
MEDIA_ROOT = '/home/acresearch/djangoProjects/fringe/fringe/images'
MEDIA_URL = 'http://localhost:8000/images/'
urlpatterns = patterns('',
(r'^submit/$', submit),
(r'^receipt/$', receipt),
(r'^images/(?P<path>.*)$', 'django.views.static.serve', {'document_root' : settings.MEDIA_ROOT}),
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})
class SubmitForm(forms.Form):
email = forms.EmailField(required=True, label='Email address:')
uploadedfile = forms.FileField(required=True, label='File to upload:')
<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>
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