表格和Photologue的问题

时间:2011-06-22 14:02:15

标签: python django django-models django-views pinax

我正在使用Pinax创建一个新项目。对于这个项目,我需要创建一个新的应用程序'Business',它与Profiles非常相似,但是他们希望将所有内容与企业分开。

我正在尝试让管理员能够更改商家资料的徽标或“头像”。我使用Photologue的ImageModel类来控制图像上传等,但我遇到了一个问题。在浏览表单时,表单会经过并重定向,但图像实际上并未更新。当你通过django管理员时,图片上传很好。

如果有人可以看看是否有东西丢失,我已经盯着它看了太久,所以我需要一双新鲜的眼睛。

Business Models.py

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")

我的views.py

@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 %}
...

谢谢大家,看一看。

史蒂夫

1 个答案:

答案 0 :(得分:0)

ChangeLogoForm的模型是BusinessLogo,但在调用它的构造函数时,你传递了Business个实例:

b = get_object_or_404(Business, pk = bus_id)
...
form = ChangeLogoForm(request.POST, request.FILES, instance = b)

(您应该使用OneToOneField字段而不是ForeignKey