1

**I am a newbie to Django and jQuery. I want client side validations in my django project. This simple project is uploading a file and displaying the contents. I am currently just checking that the file field is not empty but it is not working.

Moreover, I want to add the code to validate the file such that it can only upload .txt and image files as well as the maximum size of file must not exceed 500KB. How can I add these validations in my .js file?**

Template

{% load staticfiles %}
       <link rel="stylesheet" type="text/css" href="{% static 'FormStyle.css'%}"/>
       <script type = 'text/javascript' href="{% static 'validateForm.js' %}" >
     </script>

   <div class = "formArea">
   <div class = "fileForm">
      <form enctype="multipart/form-data"  method="post"> {% csrf_token %}

      <div class = "formFields">
    <label for = "file"> {{ form.file.label }} </label>
    {{ form.file }}
    {{ form.file.errors }}
   </div>

     <div class = "formFields">
    <input type="submit" value="Submit">
   </div>
  </form>
 </div>
    </div
   

validateForm.js

$(document).ready(function()    {
$('#form').validate({
       rules:   {
            file:   {
                required: true
            }
       },
       messages:    {
            file:   {
                required: 'Please Upload a File!'
            }
       }
});
});

Form.py

from django import forms
class fileForm(forms.Form):
     file = forms.FileField(label = 'Upload File' )

Views.py

from django.shortcuts import render_to_response, HttpResponse
from .forms import fileForm

def uploadFile(request):
   if(request.method == 'POST'):
       form = fileForm(request.POST, request.FILES)
      if(form.is_valid()):
         file = request.FILES['file']
         fileName = file.name
         # file.size returns file size in Bytes
         fileSize = file.size
         extension = fileName[fileName.index('.'):]
         return HttpResponse(file, content_type='text/plain')
      else:
         form = fileForm()
         return render_to_response('UploadForm.html', {'form': form})
Manpreet Ahluwalia
  • 301
  • 1
  • 3
  • 11

1 Answers1

0

First you must add this line after div tag.

   <script type = 'text/javascript' href="{% static 'validateForm.js' %}" >

I advice you create blockjs and add your js file here

Please , read this for more info

https://docs.djangoproject.com/en/1.10/ref/templates/language/#template-inheritance

Mubariz Feyziyev
  • 412
  • 4
  • 16
  • I hope you already added this link: https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js or https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js – Mubariz Feyziyev Mar 16 '17 at 07:49
  • somewhat got an idea what you are trying to suggest. @mubariz I just installed jQuery validations library (django-parsley). Now its displaying the default error message of required field. "This Field is required". It is not displaying the message that I have added in js file. – Manpreet Ahluwalia Mar 16 '17 at 09:10
  • For change error messages try this script: `` P.S:If problem solved please, mark 'this answer is usefull' :) – Mubariz Feyziyev Mar 16 '17 at 09:42
  • Can not be :) Where was you write it? – Mubariz Feyziyev Mar 16 '17 at 10:14
  • `$('#formFile').validate({ rules: { file: "required" }, });` edit your code like this and add id="formFile" to
    So: `
    {% csrf_token %}`
    – Mubariz Feyziyev Mar 16 '17 at 10:19
  • {% csrf_token %}
    {{ form.file }} {{ form.file.errors }}
    – Manpreet Ahluwalia Mar 16 '17 at 11:09
  • `` you must write this after including jquery validation file – Mubariz Feyziyev Mar 16 '17 at 13:04
  • Problem solved after importing () and () – Manpreet Ahluwalia Mar 17 '17 at 05:56
  • But now the problem is that if I download these js files and then import using It doesnt work – Manpreet Ahluwalia Mar 17 '17 at 05:59
  • Your last 2 comment denies each other. What is error? – Mubariz Feyziyev Mar 17 '17 at 06:11
  • the problem is that if I add direct http links of jQuery libraries then it works fine but if I download the libraries into my js folder and then import them using js files are not imported! – Manpreet Ahluwalia Mar 17 '17 at 06:20
  • moreover the jQuery validation script is also working in my html file only, if I write it in another js file then that file is not imported. – Manpreet Ahluwalia Mar 17 '17 at 06:22
  • https://docs.djangoproject.com/en/1.10/howto/static-files/ and http://stackoverflow.com/questions/7574759/django-cant-get-static-css-files-to-load Look at this 2 link and solve your problem – Mubariz Feyziyev Mar 17 '17 at 07:38