FileField in forms to upload files to the server

Hello there,

In this post, I will be discussing the process that we used to process and upload files to the server.

Django provides an alternative field named FileField that allow us to add files in the forms. Before doing anything we need to add something to our settings.py file.

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

These are used to create a special URL for the media files. It is useful when we are going to upload images because in this way we can attach an URL with the images. It can have various uses in the case of files.

After this, we are going to create a form in forms.py file and attach FileField to it.

class LocalScanForm(forms.Form):

    upload_from_local = forms.FileField(label='Upload from Local')

After this, we need to create a template for this.

{% extends 'scanapp/base.html' %}
{% load staticfiles %}
{% block content %}
    <form action="" method="post">
        {% csrf_token %}
        {{ form }}
        <input type="submit" value="Submit" />
    </form>
{% endblock %}

Create views and attach the URL to that. Here is the commit: https://github.com/nexB/scancode-server/commit/a60cbbbb4d96d46df1232b96f7ff871e5f0e318d

After that, we wrote the code to handle the files. We used the subprocess module to handle the stuff. The post about that is written here.

Now we are trapped on downloading stuff from an URL. We will write some code and push stuff on this blog. Till then stay tuned.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s