开发者

Problem with forms and Photologue

I'm using Pinax to create a new project. For this project I needed to create a new app 'Business' which is much like Profiles but they wanted to keep everything seperate for Businesses.

I'm trying to have the admin be able to change the logo or "avatar" for the business profile. Im using the ImageModel class from Photologue to control the image upload, etc but I ran into a problem. When going through the form, the form goes through and redirects but the image doesn't actually get updated. When you go through the django admin, the image uploads fine.

If someone could take a look and see if something is missing, I've been staring at it for too long, so I need a fresh pair of eyes.

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

My 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():
      开发者_JAVA技巧  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

And finally my 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 %}
...

Thanks everyone, for taking a look.

Steve


The ChangeLogoForm's model is BusinessLogo, but when calling it's constructor you pass it a Business instance:

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

(And you should probably use a OneToOneField field instead of ForeignKey)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜