Hello everybody.

I am a Django newbie experimenting with some stuff. I am very
impressed by the framework, that made me want to learn more about it.
I am trying to do some file upload but I have a problem.
I tried many information sources but could not find a simple example
of what I am trying to do. I have a few question...

Well, It should be very simple, I just want to upload a file on the
server on the MEDIA directory.
I use the up to date SVN version ( 5156 ).

I put sipets of code below, the things I dont anderstand are:
* The {{form.updateFile_file}} tag produces nothing the first time I
load my page. Is it normal behavior?

* The validation raises an AttributeError with message: 'dict' object
has no attribute 'startswith'
The lower part of the stack is :
/home/guillaume/dev/Python/django_src/django/db/models/fields/
__init__.py in isWithinMediaRoot
 628.  # If the raw path is passed in, validate it's under the
MEDIA_ROOT.
 629. def isWithinMediaRoot(field_data, all_data):

 630. f = os.path.abspath(os.path.join(settings.MEDIA_ROOT,
field_data)) ...

 631. if not
f.startswith(os.path.abspath(os.path.normpath(settings.MEDIA_ROOT))):
 632. raise validators.ValidationError, _("Enter a valid filename.")

* If I disable the validation, I have an error because Django tries to
insert the file data into the database as a string...

Well I probably missed something very simple, but I cannot figure
it ...

Might a Django Guru engilten me ?

Yours,
Guillaume.

----------------------------------------------------------------------------

My model :

#-----------------------------------------------------------------------------
class StockUpdateFile( models.Model ):

    updateFile  = models.FileField( upload_to = 'updatefiles/%Y/%m/
%d' )
    fdate       = models.DateField( auto_now_add = True )

    def __str__( self ):
        return "file %1-%2-%3" % str( distributor ) % str( fdate ) %
str( updateFile )

    class Admin:
        pass

--------------------------------------------------------------------------------------------------------------
The form is:

class UploadForm( forms.Form ):
    updateFile = forms.Field( widget = forms.FileInput() )

----------------------------------------------------------------------------------------------------------------
My template is:

<form enctype="multipart/form-data"  action="/stock/uploadfile/"
method="post" >

          <table>
          {{form.updateFile}}:{{form.updateFile_file}}
          </table>
          <input type="submit" value="Upload File" />
</form>
 
-----------------------------------------------------------------------------------------------------------------
And the code handling the form is:

def uploadFile(request, template='uploadfile.html' ):

    errors = {}

    form = UploadForm()

    if( request.FILES ):

        manip = StockUpdateFile.AddManipulator()
        newData = request.POST.copy()
        newData.update( request.FILES )

        errors = manip.get_validation_errors( newData )

        if( not errors):

            manip.do_html2python( newData )
            newFile = manip.save( newData )
        else:
            form = UploadForm()

    return render_to_response( template,
                               context_instance =
RequestContext( request, {'form': form,
 
'errors' : errors } ))


--~--~---------~--~----~------------~-------~--~----~
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