0

楷模:

from django.db import models
class Image(models.Model):
    advertisement = models.ForeignKey('Advertisement')
    image = ImageWithThumbsField(upload_to=store_path, blank=True)

class Advertisement(models.Model):
    #some code here

class AdvertOne(Advertisement):
    #some code again

class AdvertTwo(Advertisement):
    #and again

形式:

from models import Advertisement
from django.forms import *

class AdvertisementForm(ModelForm):
    message = CharField(max_length=400, widget=Textarea)
    image = ImageField(required=False)

问:我应该如何形成上传视图?

4

1 回答 1

0

像这样的东西应该工作。确保包含enctype="multipart/form-data"在表单标签中,以便上传文件。

def upload_advertisement(request):
    if request.method == "POST":
        form =  AdvertisementForm(request.POST,request.FILES, prefix='advertisement')
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/")
    else:    
        form =  AdvertisementForm(prefix='advertisement')

    return render_to_response('advertisementform.html', {
        'form':form,
    }, context_instance=RequestContext(request))
于 2011-06-15T15:10:25.960 回答