Python 表格和照片的问题
我正在使用Pinax创建一个新项目。对于这个项目,我需要创建一个新的应用程序“Business”,它非常类似于Profiles,但它们希望为企业保持一切独立 我正试图让管理员能够改变标志或“头像”的业务概况。我使用Photologue中的ImageModel类来控制图像上传等,但我遇到了一个问题。当浏览表单时,表单会浏览并重定向,但图像实际上不会得到更新。当您通过django管理员时,图像可以正常上传 如果有人可以看一看,看看是否有什么东西丢失了,我已经盯着它太久了,所以我需要一双新的眼睛 商业模式.pyPython 表格和照片的问题,python,django,django-models,django-views,pinax,Python,Django,Django Models,Django Views,Pinax,我正在使用Pinax创建一个新项目。对于这个项目,我需要创建一个新的应用程序“Business”,它非常类似于Profiles,但它们希望为企业保持一切独立 我正试图让管理员能够改变标志或“头像”的业务概况。我使用Photologue中的ImageModel类来控制图像上传等,但我遇到了一个问题。当浏览表单时,表单会浏览并重定向,但图像实际上不会得到更新。当您通过django管理员时,图像可以正常上传 如果有人可以看一看,看看是否有什么东西丢失了,我已经盯着它太久了,所以我需要一双新的眼睛 商业
class Business(models.Model):
name = models.CharField(verbose_name="Name", max_length=140)
desc = models.TextField(verbose_name="Description", null=True, blank=True)
bus_type = models.CharField(verbose_name="Business Type", choices=BUSINESS_TYPES, max_length=20)
location = models.CharField(_("location"), null=True, blank=True, max_length=200)
website = models.URLField(_("website"), null=True, blank=True, verify_exists=False)
created_by = models.ForeignKey(User, related_name="Created By")
admin = models.ManyToManyField(User, related_name="Admin User", null=True, blank=True)
followers = models.ManyToManyField(User, related_name="Followed by", null=True, blank=True)
date_added = models.DateField(verbose_name="Date Added")
class Meta:
verbose_name = "Business"
verbose_name_plural = "Businesses"
def __unicode__(self):
return self.name
class BusinessLogo(ImageModel):
business = models.ForeignKey(Business, related_name="Business Association")
我的观点
@login_required
def changeLogo(request, bus_id):
user = request.user
b = get_object_or_404(Business, pk = bus_id)
if request.method == 'POST':
form = ChangeLogoForm(request.POST, request.FILES, instance = b)
if form.is_valid():
biz_logo = form.save(commit=False)
biz_logo.save()
return HttpResponseRedirect('/')
else:
form = ChangeLogoForm()
return render_to_response('business/changelogo.html',
{'user': user, 'form':form, 'b':b}, context_instance=RequestContext(request))
Forms.py
class ChangeLogoForm(ModelForm):
class Meta:
model = BusinessLogo
def save(self, force_insert=False, force_update=False, commit=True):
f = super(ChangeLogoForm, self).save(commit=False)
if commit:
f.save()
print "form save method was called with commit TRUE"
return f
最后是我的changelogo.html
...
{% block body %}
<h1>Change Logo</h1>
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Upload">
</form>
{% endblock %}
...
。。。
{%block body%}
更改徽标
{%csrf_令牌%}
{{form.as_p}}
{%endblock%}
...
谢谢大家的关注
SteveChangeLogoForm的模型是
BusinessLogo
,但在调用其构造函数时,您会向其传递一个Business
实例:
b = get_object_or_404(Business, pk = bus_id)
...
form = ChangeLogoForm(request.POST, request.FILES, instance = b)
(你可能应该使用
OneToOneField
字段,而不是ForeignKey
)Steve,你的{{form.errors}在模板中哪里?我在模板中没有它。我现在就添加它。编辑:我只是添加了它,但没有给我任何错误,因为表单经过重定向。