我想创建一个带有图片上传选项的表单。我创建了包含4个字段的数据库:
网页上显示前3个字段,但图片上传字段丢失,但同时上传字段显示在管理区域,并显示在我从管理区域上传的网页上的图像。
如何在网页中显示上传选项? 这是我的完整代码:
的 Models.py
class album(models.Model):
artist = models.CharField(max_length=250)
title = models.CharField(max_length=500)
gender = models.CharField(max_length=100)
album_logo = models.FileField()
Urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^myapp/', include('myapp.urls', namespace='myapp')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
表单模板页面,其中只显示文本字段
{% for field in form %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }}</label>
<div class="col-sm-10">{{ field }}</div>
</div>
{% endfor %}
答案 0 :(得分:0)
表单处理2个文件:
主文件:
<form class="form-horizontal" action="" method="post"
enctype="multipart/form-data" novalidate>
{% csrf_token %}
{% include 'myapp/form-template.html' %}
<di class="form-group" class="col-sm-10">
<button type="submit" class="btn-btn-primary">Submit</button>
包含文件(form-template.html)
{% for field in form %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }}</label>
<div class="col-sm-10">{{ field }}</div>
</div>
{% endfor %}
在此表单的输出中,我只获取文本字段和图像上载字段。 如何在输出中呈现上传字段?