user1662290
user1662290

Reputation: 474

Django Upload From Template

I am looking into uploading a file from the html template. I've seen a fair amount of documentation including FileFields, ImageFields etc. However, ideally I do not want to rewrite my code. Currently, I have a simple form on my template and I would like to have an upload function there, where, an image will be uploaded and stored into my applications media folder and if possible added to a database.

I do know that I've probably taken a long and complex route but if anyone can help it'll be great!

html.py:

<div class="row">                 <div class="span1 offset5">                 </bR>
              <form class="form-horizontal" method="get" action="/add/submit" value="add">  
              <fieldset>              <div class="input">
              <div class="inline-inputs">
              <label>Ride Name:</label><input type="text" name="name">
              <label>Type:</label><input type="text" name="type">
              <label>Theme:</label><input type="text" name="theme">
              <label>Description:</label><textarea rows="5" name ="description"></textarea>
              <label>Author:</label><input type="text" name="author">

              <label>Date Released:</label>
              <div id="datetimepicker" class="input-append date">
                  <input type="text" name="date"></input>
                  <span class="add-on">
                  <i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
                  </span>
              </div>                      
              <label>Thread:</label><input type="text" name="thread">
              <label>Video</label><textarea rows="2" name ="video"></textarea>


                  <br><br>
                  <input class="btn btn-primary" type="submit" value="Add" />
                  </div>              </div>                      

               </fieldset> 
              </form>  
              </div>  </div>

Currently my Views.py just takes the entered data and inserts it into a database. I want to add the ability for a file to be uploaded:

def Ride_Add_Submit(request):
    name = request.GET['name']
    type = request.GET['type']
    theme = request.GET['theme']
    description = request.GET['description']
    author = request.GET['author']
    releasedate=request.GET['date']
    video=request.GET['video']
    thread=request.GET['thread']


    entry = RollerCoaster(name=name, type=type, theme=theme, description=description, releasedate=releasedate, author=author, video=video, thread=thread)
    entry.save()
    return TemplateResponse(request, 'Ride_Add.html')

Upvotes: 0

Views: 92

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599638

I don't understand why you keep talking about the template here, the template has nothing whatsoever to do with anything. The handling of the upload, like all logic, is done in the view.

The file upload overview still has all the information you need. You can ignore the parts about the Django form and checking if it's valid, and simply pass the file object to your upload handling function, which that page also explains.

However you will need to change your template so that the form element uses POST instead of GET (which is almost certainly a good idea anyway), and use enctype="multipart/form-data" as also described on that page.

Finally, I really would advise you to rewrite your code to use ModelForms. Not only would it make your code much simpler, it would also do things like validate the entry to make sure all the required fields are present and are of the right types, and so on - as well as output valid HTML (for instance, you're missing for attributes in your label tags).

Upvotes: 4

Related Questions