• We just launched and are currently in beta. Join us as we build and grow the community.

Python - Simple File Upload With Django

Rafaffaffaagagag

ROI Maximizer
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
86
Likes
27
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Simple File Upload With Django. Django is a free and open source web application framework, written in Python. The official project site describes Django as "a high-level Python Web framework that encourages rapid development and clean, pragmatic design. So let's now do the coding...

Getting Started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

After Python IDLE's is installed, open the command prompt then type "pip install Django", and hit enter.

2017-07-31_15_10_21-command_prompt_5.png


Wait for the django to be downloaded and installed at the same time. Then After that type "python -m django version" and hit enter to check if django is installed and what version of django is.

2017-07-31_15_13_09-command_prompt_5.png

Creating the App

After setting django we will now create the web app for the web server. First create a new folder named "Django File Upload", then cd to a newly created folder, then type "django-admin startproject server" and hit enter. A new folder will be created on the directory named 'server'.

2017-08-28_09_22_36-c_windows_system32_cmd.exe_.png

Running The Server

After creating a project, cd to the newly created directory, then type "manage.py runserver" and hit enter to start the server running. The "manage.py" is a command of django-admin that utilize the administrative tasks of python web framework.

Here is the image of python web server:
2017-07-28_13_32_58-settings_5.png

Note: Type '127.0.0.1:8000' in the url browser to view the server. When there is code changes in the server just (ctrl + C) to command prompt to stop the server from running, then start again to avoid errors.

Creating The Website

This time will now create the web app to display the web models. First locate the directory of the app via command prompt cd, then type "manage.py startapp file" and hit enter. A new directory will be create inside the app named "file".

2017-08-28_09_42_22-c_windows_system32_cmd.exe_.png

Setting up The URL

This time will now create a url address to connect the app from the server. First Go to server directory, then open urls via Python IDLE's or any text editor. Then import "include" module beside the url module and import additional module to make a redirect url to your site "from . import views". After that copy/paste the code below inside the urlpatterns.

  1. url(

    r'^$'

    ,

    views.index_redirect

    ,

    name=

    'views_redirect'

    )

    ,
  2. url(

    r'^file/$'

    ,

    include(

    'file.urls'

    )

    )

    ,

Then import two modules called:
  1. from

    django.conf

    import

    settings
  2. from

    django.conf

    .urls

    .static

    import

    static

After that create a statement to access the media root
  1. if

    settings.DEBUG

    :
  2. urlpatterns +=

    static(

    settings.STATIC_URL

    ,

    document_root=

    settings.STATIC_ROOT

    )
  3. urlpatterns +=

    static(

    settings.MEDIA_URL

    ,

    document_root=

    settings.MEDIA_ROOT

    )

It will be look like this:

  1. from

    django.conf

    .urls

    import

    include,

    url
  2. from

    django.contrib

    import

    admin
  3. from

    django.conf

    import

    settings
  4. from

    django.conf

    .urls

    .static

    import

    static
  5. from

    . import

    views

  6. urlpatterns =

    [
  7. url(

    r'^$'

    ,

    views.index_redirect

    ,

    name=

    'views_redirect'

    )

    ,
  8. url(

    r'^file/'

    ,

    include(

    'file.urls'

    )

    )

    ,
  9. url(

    r'^admin/'

    ,

    admin.site

    .urls

    )

    ,
  10. ]

  11. if

    settings.DEBUG

    :
  12. urlpatterns +=

    static(

    settings.STATIC_URL

    ,

    document_root=

    settings.STATIC_ROOT

    )
  13. urlpatterns +=

    static(

    settings.MEDIA_URL

    ,

    document_root=

    settings.MEDIA_ROOT

    )

Then after that create a view that will catch the redirect url. To do that create a file "views.py" then copy/paste the code below and save it as "views.py".

  1. from

    django.shortcuts

    import

    redirect

  2. def

    index_redirect(

    request)

    :
  3. return

    redirect(

    '/file/'

    )

Creating The Path For The Pages

Now that we set the connect we will now create a path for the web pages. All you have to do first is to go to file directory, then copy/paste the code below and save it inside "file" directory named 'urls.py' The file name must be urls.py or else there will be an error in the code.

  1. from

    django.conf

    .urls

    import

    url
  2. from

    . import

    views

  3. urlpatterns =

    [
  4. url(

    r'^$'

    ,

    views.index

    ,

    name=

    'index'

    )

    ,
  5. ]

Creating A Static Folder

This time we will create a directory that store an external file. First go to the file directory then create a directory called "static", after that create a sub directory called "file". You'll notice that it is the same as your main app directory name, to assure the absolute link. This is where you import the css, js, etc directory.

Creating The Views

The views contains the interface of the website. This is where you assign the html code for rendering it to django framework and contains a methods that call a specific functions. To do that first open the views.py, the copy/paste the code below.

  1. from

    django.shortcuts

    import

    render
  2. from

    django.core

    .files

    .storage

    import

    FileSystemStorage
  3. from

    .models

    import

    File
  4. import

    os

    ,

    datetime

  5. # Create your views here.

  6. def

    index(

    request)

    :
  7. if

    request.method

    ==

    'POST'

    and

    request.FILES

    [

    'file'

    ]

    :
  8. upload_file =

    request.FILES

    [

    'file'

    ]
  9. extension =

    os

    .path

    .splitext

    (

    upload_file.name

    )

    [

    1

    ]
  10. rename =

    datetime

    .datetime

    .now

    (

    )

    .strftime

    (

    "%Y_%m_%d %H_%M_%S"

    )

    + extension
  11. fss =

    FileSystemStorage(

    )
  12. filename =

    fss.save

    (

    rename,

    upload_file)
  13. file

    =

    File(

    file

    =

    rename)
  14. file

    .save

    (

    )
  15. upload_file_path =

    fss.path

    (

    filename)

  16. return

    render(

    request,

    'file/index.html'

    ,

    {
  17. 'upload_file_path'

    : upload_file_path
  18. }

    )
  19. else

    :
  20. return

    render(

    request,

    'file/index.html'

    )

Registering The App To The Server

Now that we created the interface we will now then register the app to the server. To do that go to the server directory, then open "settings.py" via Python IDLE's or any text editor. Then copy/paste this script inside the INSTALLED_APP variables 'file'.

It will be like this:
  1. INSTALLED_APPS =

    [
  2. 'file'

    ,
  3. 'django.contrib.admin'

    ,
  4. 'django.contrib.auth'

    ,
  5. 'django.contrib.contenttypes'

    ,
  6. 'django.contrib.sessions'

    ,
  7. 'django.contrib.messages'

    ,
  8. 'django.contrib.staticfiles'

    ,
  9. ]

Then we will create an absolute link that give access to MEDIA root folder, copy the code below then paste it below the STATIC_URL
  1. MEDIA_ROOT =

    os

    .path

    .join

    (

    BASE_DIR,

    'media'

    )
  2. MEDIA_URL =

    '/media/'

Creating The Mark up Language

Now we will create the html interface for the django framework. First go to file directory, then create a directory called "templates" and create a sub directory on it called file.

base.html
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. {% load static %}
  6. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "{% static 'file/css/bootstrap.css' %}"

    /

    >
  7. </

    head

    >
  8. <body

    >
  9. <nav class

    =

    "navbar navbar-default"

    >
  10. <div

    class

    =

    "container-fluid"

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodster.com"

    >

    Sourcecodester</

    a

    >
  12. </

    div

    >
  13. </

    nav>
  14. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  15. <div

    class

    =

    "col-md-6 well"

    >
  16. <h3

    class

    =

    "text-primary"

    >

    Python - Simple File Upload With Django</

    h3

    >
  17. <hr

    style

    =

    "border-top:1px dotted #000;"

    /

    >
  18. {% block body %}
  19. {% endblock %}
  20. </

    div

    >
  21. </

    body

    >
  22. </

    html

    >

Save it as "base.html" inside the file directory "sub directory of templates".

index.html
  1. {% extends 'file/base.html' %}
  2. {% block body %}
  3. <form

    method

    =

    "POST"

    enctype

    =

    "multipart/form-data"

    >
  4. {% csrf_token %}
  5. <div

    class

    =

    "form-group"

    >
  6. <input

    type

    =

    "file"

    name

    =

    "file"

    required=

    "required"

    /

    >
  7. <br

    /

    >
  8. <button

    type

    =

    "submit"

    class

    =

    "btn btn-sm btn-primary"

    ><span

    class

    =

    "glyphicon glyphicon-upload"

    ></

    span

    >

    Upload</

    button

    >
  9. </

    div

    >
  10. </

    form

    >

  11. {% if upload_file_path %}
  12. Uploaded at: <label

    class

    =

    "text-primary"

    >

    {{ upload_file_path }}</

    label

    >
  13. {% endif %}

  14. {% endblock %}

Save it as "index.html" inside the file directory "sub directory of templates".

Migrating The App To The Server

Now that we done in setting up all the necessary needed, we will now then make a migration and migrate the app to the server at the same time. To do that open the command prompt then cd to the "server" directory, then type "manage.py makemigrations" and hit enter. After that type again "manage.py migrate" then hit enter.

2017-08-28_11_12_07-select_c_windows_system32_cmd.exe_.png


Now try to run the server again, and see if all things are done.

There you have it we successfully created a Simple File Upload With Django. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

333,651

333,659

Top