lundi 29 juin 2015

Django 1.8 File Upload Example

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 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):
Here is my 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 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 = = False)
#     #We Might Do Something Here
#     email = form.cleaned_data['email']
#     new_test_old, created = Test.objects.get_or_create(email = email)
#     #

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

This is my 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

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(,

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?

Aucun commentaire:

Enregistrer un commentaire