Guest User

Untitled

a guest
Jul 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. myproject/
  2. media/
  3. myapp/
  4. templates/
  5. myapp/
  6. list.html
  7. forms.py
  8. models.py
  9. urls.py
  10. views.py
  11. __init__.py
  12. manage.py
  13. settings.py
  14. urls.py
  15.  
  16. ...
  17. MEDIA_ROOT = '/path/to/myproject/media/'
  18. MEDIA_URL = '/media/'
  19. ...
  20.  
  21. ...
  22. class Document(models.Model):
  23. docfile = models.FileField(upload_to='documents/%Y/%m/%d')
  24. ...
  25.  
  26. ...
  27. class DocumentForm(forms.Form):
  28. docfile = forms.FileField(
  29. label='Select a file',
  30. help_text='max. 42 megabytes'
  31. )
  32. ...
  33.  
  34. ...
  35. from django.shortcuts import render_to_response
  36. from django.template import RequestContext
  37. from django.http import HttpResponseRedirect
  38.  
  39. from myproject.myapp.models import Document
  40. from myproject.myapp.forms import DocumentForm
  41. ...
  42. def list(request):
  43. # Handle file upload
  44. if request.method == 'POST':
  45. form = DocumentForm(request.POST, request.FILES)
  46. if form.is_valid():
  47. newdoc = Document(docfile = request.FILES['docfile'])
  48. newdoc.save()
  49.  
  50. # Redirect to the document list after POST
  51. return HttpResponseRedirect(reverse('myapp.views.list'))
  52. else:
  53. form = DocumentForm() # A empty, unbound form
  54.  
  55. # Load documents for the list page
  56. documents = Document.objects.all()
  57.  
  58. # Render list page with the documents and the form
  59. return render_to_response(
  60. 'myapp/list.html',
  61. {'documents': documents, 'form': form},
  62. context_instance=RequestContext(request)
  63. )
  64.  
  65. from django.conf.urls.defaults import patterns, include, url
  66. from django.conf import settings
  67. from django.conf.urls.static import static
  68.  
  69. urlpatterns = patterns('',
  70. (r'^', include('myapp.urls')),
  71. ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  72.  
  73. from django.conf.urls.defaults import patterns, url
  74.  
  75. urlpatterns = patterns('myapp.views',
  76. url(r'^list/$', 'listview', name='list'),
  77. )
  78.  
  79. ...
  80. <!-- List of uploaded documents -->
  81. {% if documents %}
  82. <ul>
  83. {% for document in documents %}
  84. <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a></li>
  85. {% endfor %}
  86. </ul>
  87. {% else %}
  88. <p>No documents.</p>
  89. {% endif %}
  90.  
  91. <!-- Upload form. Note enctype attribute! -->
  92. <form action="{% url list %}" method="post" enctype="multipart/form-data">
  93. {% csrf_token %}
  94. {{ form.non_field_errors }}
  95. {{ form.docfile.help_text }}
  96. {{ form.docfile.label_tag }}
  97. {{ form.docfile.errors }}
  98. {{ form.docfile }}
  99. <input type="submit" value="Upload" />
  100. </form>
  101. ...
  102.  
  103. <form method="post" enctype="multipart/form-data">
  104. <input type="file" name="myfile" />
  105. <input type="submit" name="submit" value="Upload" />
  106. </form>
  107.  
  108. def myview(request):
  109. request.FILES['myfile'] # this is my file
  110.  
  111. import tempfile
  112. import shutil
  113.  
  114. FILE_UPLOAD_DIR = '/home/imran/uploads'
  115.  
  116. def handle_uploaded_file(source):
  117. fd, filepath = tempfile.mkstemp(prefix=source.name, dir=FILE_UPLOAD_DIR)
  118. with open(filepath, 'wb') as dest:
  119. shutil.copyfileobj(source, dest)
  120. return filepath
  121.  
  122. entry = form.save()
  123.  
  124. # save uploaded file
  125. if request.FILES['myfile']:
  126. entry.myfile.save(request.FILES['myfile']._name, request.FILES['myfile'], True)
Add Comment
Please, Sign In to add comment