在开发环境中我可以加载表单并更新任何文本字段,django会自动处理静态图像。
在webservice处理的生产静态图像中,它在表单更新视图中加载图像,但它再次要求在每次更新中选择图像。它无法识别现有的图像,我知道我必须在视图中处理它,但不确定它是如何完成的。请帮助解决它。
models.py
class Shop(models.Model):
name = models.CharField('Shop Name', max_length=200)
address = models.CharField('Door numb, Building', max_length=100 )
street = models.CharField('Street Name', max_length=100 )
sublocal = models.CharField('Area (if any)', max_length=100,blank=True,null=True )
location = models.CharField('Locality', max_length=100 )
//
landmark = models.CharField('Landmark/Complex/Mall (If any, )', max_length=500, null=True, blank=True)
phone = models.CharField('Phone Number', max_length=100)
//
pfimage = models.ImageField('Profile Photo', upload_to='shop/%Y',)
pfimage_thumb = ImageSpecField(source='pfimage',
processors=[Transpose(),ResizeToFill(200, 150), Adjust(contrast=1.2, sharpness=1.1)],
format='JPEG',
options={'quality': 95})
uid =models.CharField('Shop URL (optional) - shopmapy.com/< pincode>/', max_length=100, blank=True)
views.py
@csrf_protect
@login_required
def shopreregister(request, qs):
vshop = Shop.objects.filter(user__username=request.user)
shop = get_object_or_404(vshop, pk=qs)
print shop
form = ShopForm(data = request.POST or None, instance=shop)
form.fields['name'].widget.attrs['readonly'] = True
form.fields['uid'].widget.attrs['editable'] = True
if request.method == 'POST':
if form.is_valid():
#form['user'] = request.user
f = form.save(commit=False)
f.user = request.user
f.company = Company.objects.get(name='Common')
f.save()
form.save_m2m()
shopid = form.instance.id
name = form.cleaned_data['name']
return redirect('vendor-shop' , shopid) #thing='%s Registered' %name)
return render(request, 'shopregister.html', {'form':form})
模板:
{% block content %}
<form method="post" action="." enctype="multipart/form-data">{% csrf_token %}
<table border="0">
{{ form }}
</table>
<input type="submit" value="Submit" />
</form>
{% endblock %}