1

I’m new to the Django development platform. I have been struggling to create a basic file upload app in Django 1.8.2 (latest version). I could not find any up-to-date example/snippet. So I appreciate if you could post here or refer me to a minimal but complete (Model, View, Template) example code to do so.

I’ve observed many other examples online but I notice that my version does not have the MEDIA_ROOT or MEDIA_URL attributes by default. For example:

MEDIA_ROOT = '/path/to/myproject/media/'
MEDIA_URL = '/media/'

Here is my Models.py page

from django.db import models

# Create your models here.

class Test(models.Model):
   first_name = models.SlugField()
   last_name = models.SlugField()
   email = models.EmailField()
   timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
   updated = models.DateTimeField(auto_now_add = False, auto_now = True)

def __unicode__(self):
    return self.email

Here is my Forms.py page

from django import forms
from .models import Test

class EmailForm(forms.Form):
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)
    email = forms.EmailField()

class TestForm(forms.ModelForm):
    class Meta:
    model = Test

Here is my Views.py page

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

# Create your views here.
from .forms import EmailForm, TestForm
from .models import Test

def home(request):
   # print request.POST["email"], request.POST["email2"]

   form = EmailForm(request.POST or None)
   if form.is_valid():
      first_name = form.cleaned_data['first_name']
      last_name = form.cleaned_data['last_name']
      email = form.cleaned_data['email']
      new_test, created = Test.objects.get_or_create(email = email,        first_name = first_name, last_name = last_name)

    # # This is using Model Forms
    # form = TestForm(request.POST or None)
    # if form.is_valid():
    #     new_test = form.save(commit = False)
    #     #We Might Do Something Here
    #     email = form.cleaned_data['email']
    #     new_test_old, created = Test.objects.get_or_create(email = email)
    #     # new_test.save()

    context = {"form": form}
    template = "home.html"
    return render(request, template, context)

This is my URLs.py page

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
url(r'^$', 'tests.views.home', name='home'),
# url(r'^home2/$', 'lwc.views.home2', name='home'),
# url(r'^blog/', include('blog.urls')),

#This admin can be changed to anything but the
#link must also be changed to access admin area
url(r'^admin/', include(admin.site.urls)),
)

The objective of my app is to allow users to upload a file and submit to the associated SQLite database. How do I accomplish this without a MEDIA_ROOT or MEDIA_URL?

Robin Curbelo
  • 1,323
  • 1
  • 11
  • 21
  • Welcome to Stackoverlfow! You will greatly increase your chances of getting an answer for your question if you include your input, what you have tried, your expected output vs. your actual output and the full stack trace of any errors you receive. You can also read [this guide](http://stackoverflow.com/help/mcve) – kylieCatt Jun 29 '15 at 15:56
  • You'll want to create a field with your specific upload_to location, https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.FileField | When you instance the form from your view add 'request.FILES or None, ...' | In your template, set the form with enctype="multipart/form-data" – Ryan Serra Jun 29 '15 at 18:44
  • It has been answered see this https://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example?rq=1 – 4rshdeep Jun 04 '17 at 06:27

0 Answers0