Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- myproject/
- media/
- myapp/
- templates/
- myapp/
- list.html
- forms.py
- models.py
- urls.py
- views.py
- __init__.py
- manage.py
- settings.py
- urls.py
- ...
- MEDIA_ROOT = '/path/to/myproject/media/'
- MEDIA_URL = '/media/'
- ...
- ...
- class Document(models.Model):
- docfile = models.FileField(upload_to='documents/%Y/%m/%d')
- ...
- ...
- class DocumentForm(forms.Form):
- docfile = forms.FileField(
- label='Select a file',
- help_text='max. 42 megabytes'
- )
- ...
- ...
- from django.shortcuts import render_to_response
- from django.template import RequestContext
- from django.http import HttpResponseRedirect
- from myproject.myapp.models import Document
- from myproject.myapp.forms import DocumentForm
- ...
- def list(request):
- # Handle file upload
- if request.method == 'POST':
- form = DocumentForm(request.POST, request.FILES)
- if form.is_valid():
- newdoc = Document(docfile = request.FILES['docfile'])
- newdoc.save()
- # Redirect to the document list after POST
- return HttpResponseRedirect(reverse('myapp.views.list'))
- else:
- form = DocumentForm() # A empty, unbound form
- # Load documents for the list page
- documents = Document.objects.all()
- # Render list page with the documents and the form
- return render_to_response(
- 'myapp/list.html',
- {'documents': documents, 'form': form},
- context_instance=RequestContext(request)
- )
- from django.conf.urls.defaults import patterns, include, url
- from django.conf import settings
- from django.conf.urls.static import static
- urlpatterns = patterns('',
- (r'^', include('myapp.urls')),
- ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
- from django.conf.urls.defaults import patterns, url
- urlpatterns = patterns('myapp.views',
- url(r'^list/$', 'listview', name='list'),
- )
- ...
- <!-- List of uploaded documents -->
- {% if documents %}
- <ul>
- {% for document in documents %}
- <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>
- {% endfor %}
- </ul>
- {% else %}
- <p>No documents.</p>
- {% endif %}
- <!-- Upload form. Note enctype attribute! -->
- <form action="{% url list %}" method="post" enctype="multipart/form-data">
- {% csrf_token %}
- {{ form.non_field_errors }}
- {{ form.docfile.help_text }}
- {{ form.docfile.label_tag }}
- {{ form.docfile.errors }}
- {{ form.docfile }}
- <input type="submit" value="Upload" />
- </form>
- ...
- <form method="post" enctype="multipart/form-data">
- <input type="file" name="myfile" />
- <input type="submit" name="submit" value="Upload" />
- </form>
- def myview(request):
- request.FILES['myfile'] # this is my file
- import tempfile
- import shutil
- FILE_UPLOAD_DIR = '/home/imran/uploads'
- def handle_uploaded_file(source):
- fd, filepath = tempfile.mkstemp(prefix=source.name, dir=FILE_UPLOAD_DIR)
- with open(filepath, 'wb') as dest:
- shutil.copyfileobj(source, dest)
- return filepath
- entry = form.save()
- # save uploaded file
- if request.FILES['myfile']:
- entry.myfile.save(request.FILES['myfile']._name, request.FILES['myfile'], True)
Add Comment
Please, Sign In to add comment