Thank you for your help.

I'm baffled. The validation error is:

<ul class="errorlist"><li>content<ul class="errorlist"><li>This field
is required.</li></ul></li></ul>

which is fair enough... but it is in the form and I've selected a
file, clicked open, and clicked submit...


I'm probably doing something mind-meltingly stupid.

hmmm.

Glad the rest of it seems ok, though.

On Apr 16, 1:41 pm, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
> On Wed, Apr 16, 2008 at 6:55 AM, sparky <[EMAIL PROTECTED]> wrote:
>
> > Hello all,
>
> > I have a slightly unusual requirement: I want to use a FileField in a
> > form but with a TextField in the model. (The content being uploaded is
> > a big bit of flat text, but I want to store it in the database, not as
> > a file.)
>
> > The problem is that with the code that I have together the
> > request.FILES parameter is empty, so the form fails validation, any
> > suggestions as to where I'm going wrong, thanks.
>
> I do not see any reason why what you have posted below would fail form
> validation.  request.FILES will be populated, assuming you actually choose a
> file before you click "Submit".  Are you really seeing a problem with form
> validation failing?  If so, what exactly is the validation error message ?
> "This field is required." or "The submitted file is empty" or ...?
>
> The problem I do see in your code below is that you do not put the content
> of the uploaded file in your model as you state you want to.  When your form
> is validated, cleaned_data['content'] will be an UploadedFile.  If you want
> to access the content of the UploadedFile, you need to use
> cleaned_data['content'].content.  As you have coded it below I believe you
> will get the name of your uploaded file put into your model's content field.
>
> Karen
>
> > sparky
>
> > the form:
> > class SubmissionForm(forms.Form):
> >    title = forms.CharField(max_length=100)
> >    description = forms.CharField(widget=forms.Textarea)
> >    content = forms.FileField(widget=forms.FileInput)
>
> > the model:
>
> > class Submission(models.Model):
> >    """
> >    Submission model
> >    """
> >    creator = models.ForeignKey(User)
> >    created_datetime  = models.DateTimeField(auto_now_add=True)
> >    title = models.CharField(max_length=100)
> >    description = models.TextField()
> >    content = models.TextField()
>
> > The view code:
> >    if request.method == 'POST':
> >        form = SubmissionForm(request.POST, request.FILES)
> >        if form.is_valid():
> >            s = Submission(creator=request.user,
> >                        created_datetime=datetime.datetime.now(),
> >                        title=form.cleaned_data['title'],
> >                        description=form.cleaned_data['description'],
> >                        content=form.cleaned_data['content'])
>
> > the template:
>
> >    <form enctype="multipart/form-data" action="." method="POST">
> >        <table>
> >            {{ form.as_p }}
> >        </table>
> >        <p><input type="submit" value="Submit"></p>
> >    </form>
--~--~---------~--~----~------------~-------~--~----~
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