表格和 Photologue 的问题

发布于 2024-11-16 10:24:49 字数 2522 浏览 5 评论 0原文

我正在使用 Pinax 创建一个新项目。对于这个项目,我需要创建一个新的应用程序“业务”,它很像个人资料,但他们希望为企业保留所有内容。

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

如果有人可以看一下是否缺少什么东西,我已经盯着它看了太久了,所以我需要一双新的眼睛。

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

谢谢大家的浏览。

史蒂夫

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():
        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心作怪 2024-11-23 10:24:49

ChangeLogoForm 的模型是 BusinessLogo,但是在调用它的构造函数时,您向其传递一个 Business 实例:(

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

并且您可能应该使用 >OneToOneField 字段而不是 ForeignKey

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文