A Generic File Model for all kind of file attachment with AJAX fileupload and drag & drop feature.
- Attach files to any Model across your project
- Include file upload field in your django templates using our templatetags
- Retrieve back the list of files attached to your Object using our templatetags
pip install django-generic-file
Add "genericfile" to your INSTALLED_APPS and migrate.
Include following static files in your template:
<link rel="stylesheet" type="text/css" href="{% static 'genericfile/genericfile.css' %}"> <script src="{% static 'genericfile/genericfile.js' %}"></script>
At the top of your template load our template tags:
{% load genericfiletags %}
example 1: In Your Add Forms (basically where object is not yet created / unknown):
<form action='.' method='post'> {% csrf_token %} {{ form }} {% get_genericfile_form %} <button type='submit' class="btn blue">Submit</button> </form> # then in POST method of your view, from genericfile.views import update_genericfile update_genericfile(self.request.POST, self.object)
example 2: In Your Edit Form (where object is known):
<form action='.' method='post'> {% csrf_token %} {{ form }} {% get_genericfile_form host_object=form.instance %} <button type='submit' class="btn blue">Submit</button> </form>
maxFileCount - to Restrict Number of files:
{% get_genericfile_form maxFileCount=1 %} # Default is NoLimit
allowedTypes - to Restrict File types:
{% get_genericfile_form allowedTypes="jpg,jpeg,png,gif,doc,pdf,zip,html,txt,docx" %} # Default is AnyFiles
{% get_genericfile_list host_object=object as attachments %} <ul> {% for file in attachments %} <li><a href="{{file.attachment.url}}" target="_blank">{{file.get_name}}</a></li> {% empty %} <li>No files found</li> {% endfor %} </ul>
Include if you not have included them already in you html file
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">