USER
USER

Reputation: 17

django content type and file size restriction

--models.py--

class resmodel(models.Model):
    fname = models.CharField(max_length=20)
    lname = models.CharField(max_length=20)
    file_header = models.CharField(max_length=20)
    upload_file = ContentTypeRestrictedFileField(
                    upload_to='documents', ]
                    content_types=['documents/msword', 'documents/pdf', 'documents/vnd.oasis.opendocument.text'],
                    max_upload_size=5242880,blank=True, null=True)
    imag = models.ImageField(upload_to='images')

--views.py--

def resview(request):
    if request.method == "POST":
        fname = request.POST.get('fname')
        lname = request.POST.get('lname')
        file_header = request.POST.get('file_header')
        upload_resume = request.FILES['upload_resume']
        imag = request.FILES['im']

        if upload_file and imag:
            details = resmodel(file_header=file_header, fname=fname, lname=lname, upload_file=upload_file, imag=imag)
            details.save()

        return HttpResponseRedirect('/profile/save/success/')
    else:
        return render_to_response('file.html')

--file.html--

<form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
<table border="0" align="center" cellsapcing="1" cellspadding="1">

    <tr>
        <td colspan="2"><h3> Personal Information</h3></td>

        <td>First Name</td>
        <td><input type="text" name="fname" /></td>
        <td>Last Name</td>
        <td><input type="text" name="lname" /></td>
        <td>Resume Header<sub>(250 words max)</sub></td>
        <td><input type="text" name="file_header"></td>
        <td>Upload Resume<sub>(.doc/.docx, rtf, pdf, txt)</sub></td>
        <td><input type="file" name="upload_resume" ></td>
        <td>Upload image</td>
        <td><input type="file" name="im" ></td>

        <td>
            <input type="submit" value="SUBMIT">&nbsp;&nbsp;
            <input type="reset" value="RESET">
        </td>
    </tr>
</table>
</form>

For "ContentTypeRestrictedFileField" i followed this link

But here i am able to upload all files and folders in both imag and upload_file field. How to restrict this? Could anyone help me to fix this? Thanks.

Upvotes: 0

Views: 485

Answers (1)

Eduard Iskandarov
Eduard Iskandarov

Reputation: 862

You have to use form validation.

from django.forms import models as model_forms

def resview(request):
    if request.method == "POST":
        form_class = model_forms.modelform_factory(resmodel)
        form = form_class(request.POST)
        if form.is_valid():
            details = form.save()
        return HttpResponseRedirect('/profile/save/success/')
    else:
        return render_to_response('file.html')

But pay attention that this is basic form usage.

In future you should to pass form to template context and fill template with form data to display form inputs, labels and validation errors.

More documentation here Creating forms from models and here Working with forms

Upvotes: 1

Related Questions