Stephen Mizell schreef:
>> That line is executed exactly once, at the time the file is imported. So
>> it is not dynamic in any way.
>>
>> (I seem to be saying that at least every other day here. It's normal
>> Python behaviour, people. Please stop hoping it were otherwise!)
> 
> Thanks for the help.  I didn't call it dynamic, though.  What are you
> referring to?
> 
> I'll look through the fields.py file and see what I can do.  I'm
> pretty new to Django and even Python, so I'm guessing I won't have
> much luck ;)

The way i solved it was by changing the data in my view.
Mind that i also save the image file manually.
But it works.
I get the user data because i use the authentication layer of Django.
It's described in the docs.

If you want to validate the size of the image for instance,
add this function to the ImageForm:

def clean_image(self):
    # validate data here
    return self.clean_data["image"]


This is the form and the view function:

class ImageForm(forms.Form):
    name = forms.CharField(max_length=60)
    image = forms.Field( widget=forms.FileInput() )

def add_icon(request):
    if request.method == 'POST':
        form = ImageForm(request.POST)
        # prepare the variables to manage the path of the image
        sep = os.path.sep
        userpart = "%s_%s" % (request.user.id,request.user.username)
        filename = 'icon_%d.jpg' % request.user.id
        image_path = "img" + sep + userpart + sep + "icons" + sep

        form.data["image"] = image_path + filename

        if form.is_valid():
            icon = form.save(commit=False)
            new_data = request.POST.copy()
            # if we have an image, upload it
            if new_data.get('image', None):
                # create the path, make the dirs
                path = settings.MEDIA_ROOT + sep + image_path
                os.makedirs(path)
                path = path + filename
                # save the image
                try:
                    fout = file(path,'wb')
                    fout.write(request.FILES["image"]["content"])
                finally:
                    fout.close()
            icon.save()
            return HttpResponseRedirect("/")
    else:
        errors = new_data = {}
        form = ImageForm()
    return render_to_response('main/icon/add_icon3.html',
                {'form': form, 'user':request.user.id},
                 context_instance=RequestContext(request))

Regards,
Benedict


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to