Question

I'm trying to create a simple upload form for my project in Django 1.5 an Python 2.7.

This is my File class:

class File(models.Model):
    middleschool = 'MS'
    highschool = 'HS'
    university = 'U'
    blank = '-'

    school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)

    name = models.CharField(max_length = 30, primary_key=True, blank=False, null=False)
    description = models.CharField(max_length = 140, blank=False, null=False)
    school = models.CharField(max_length = 30, choices = school_choices, default = blank)
    subject = models.ForeignKey(Subject)
    user = models.ForeignKey(User)
    rating = models.DecimalField(max_digits=2, decimal_places=0, default = 0)
    price = models.DecimalField(max_digits=2, decimal_places=1, default = 0, blank=True, null=True)
    file = models.FileField(upload_to= "/file/")

this is the form:

class UploadFileForm(forms.Form):
    middleschool = 'MS'
    highschool = 'HS'
    university = 'U'
    blank = '-'

    school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)

    name = forms.CharField(max_length = 30, required = True)
    file = forms.FileField()
    description = forms.CharField(max_length = 140, required = False, label='Breif description of the files content')
    school = forms.ChoiceField(choices = school_choices, required=False, label='What level is the material that are you uploading?', initial = blank)
    subject = forms.ModelChoiceField(queryset=Subject.objects.order_by('?'), required=False, label='What subject this file is about?')
    price = forms.IntegerField(required=False)

this is the view:

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            new_file = File(file = request.FILE['file'])
            cd = form.cleaned_data
            new_file.name = cd['name']
            new_file.description = cd['description']
            new_file.school = cd['school']
            new_file.subject = cd['subject']
            new_file.price = cd['price']
            new_file.rating = '0.0'
            new_file.user = request.user
            new_file.save()
            form = Search()
            return render(request, 'home.html', {'form': form, 'request': request})
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form, 'request': request})

and this is the relative HTML

{% if request.user.is_authenticated %}
    <form action="" method="post">
    {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Upload">
        <input type="reset" value="Reset">
    </form>
{% else %}
    <p>You must be logged to upload a file</p>
{% endif %}

My app path is: C:/Users/User/Desktop/site_is/app_is/ and I want hose files saved in the folder: C:/Users/User/Desktop/site_is/app_is/static/file/. In my Setting.py I set:

MEDIA_ROOT = 'C:/Users/User/Desktop/site_is/app_is/static/file/'
MEDIA_URL = '/file/'
STATIC_ROOT = 'C:/Users/User/Desktop/site_is/app_is/static/'
STATIC_URL = '/static/'

The problem is: when i select the file and hit the upload button the FileField empties itself and the form raise an error since that field is required.

I fear I'm doing something wrong with the media/static paths in the Setting.py because the view syntax it's the same as the one in the Django docmentation but I really don't know how to solve this problem.

Was it helpful?

Solution

You should specify enctype in form tag to allow file upload.

<form action="" method="post" enctype="multipart/form-data">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top