Django: Downloading uploaded files - django

I have form details in this question Django: Adding files to a form gives multiple argument error
How to download the uploaded file. When i go to edit view of the form, i can see uploaded file url, but its not downloading.
What setting to be changed for development and production mode?
Error upon clicking link:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/Certificate.docx
Using the URLconf defined in tiktant.urls, Django tried these URL patterns, in this order:
^ ^$ [name='home']
^ ^login/$ [name='login']
^ ^logout/$ [name='logout']
^ ^logout_then_login/$ [name='logout_then_login']
^ ^dashboard/$ [name='dashboard']
The current URL, media/Certificate.docx, didn't match any of these.

The media url is not showing in the root urls.py file. You need to add the below code in the urls.py file to enable the same.
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Note: Please don't forget to add the MEDIA_ROOT and MEDIA_URL in settings.py file. For downloading a file, you need to write some more code.

make sure you added the following lines in your project
settings.py
import os
def root(x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x)
MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
TEMPLATE_CONTEXT_PROCESSORS = (
'-----------------------'
'django.core.context_processors.media',
)
urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
urlpatterns += patterns('',url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)

Related

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/media/Profile_pics/itachi.jpg

I am trying to load profile picture for each user default profile picture for user is default.jpg
But happening is i am unable to load profile picture Page Not found Error occurs.
After running my program the media directory created in my project file with
Profile_picture directory and i saved my default.jpg in media and
uploaded profile picture are saved in Profile_picture
models.py:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_picture = models.ImageField(default='default.jpg',upload_to='Profile_pics')
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
from django.contrib import admin
from django.urls import path,include
from users import views as user_view
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',include('CovidHelp.urls')),
path('profile/',include('users.urls')),
path('register/',user_view.register,name='register'),
path('login/',auth_views.LoginView.as_view(template_name='users/login.html'),name='login'),
path('logout/',auth_views.LogoutView.as_view(template_name='users/logout.html'),name='logout'),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
The Error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/Profile_pics/itachi.jpg
Using the URLconf defined in MyCovidHelp.urls, Django tried these URL patterns, in this order:
[name='Home']
about/ [name='About']
profile/
register/ [name='register']
login/ [name='login']
logout/ [name='logout']
admin/
^static/(?P<path>.*)$
The current path, media/Profile_pics/itachi.jpg, didn't match any of these.
last line of urlpatterns in urls.py should be like this
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

MEDIA_URL Page 404 Error Django

I am running on a local server and when I go to http://127.0.0.1:8000/media/ in my browser it says page not found.
Settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
Root URL:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^polls/', include('mysite.polls.urls')),
url(r'^admin/', admin.site.urls),
url(r'^submit/', include('mysite.uploads.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Model for User Upload:
from django.db import models
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)
Following the Django documentation:
Serving files uploaded by a user during development
During development, you can serve user-uploaded media files from MEDIA_ROOT using the django.contrib.staticfiles.views.serve() view.
This is not suitable for production use! For some common deployment strategies, see Deploying static files.
For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
https://docs.djangoproject.com/en/1.10/howto/static-files/
As pointed above in the comments by Alasdair, This is the normal behaviour of Django. If you visit the full file path like 127.0.0.1/media/file.jpg, Django will render the file instead of raising a 404 error.
Why is this happening?
If you look at the source of the view that serves the media/static files, you'll find these lines:
if os.path.isdir(fullpath):
if show_indexes:
return directory_index(newpath, fullpath)
raise Http404(_("Directory indexes are not allowed here."))
What it does is, it checks if the path requested is a directory or not. If yes, then it checks if the variable show_indexes is True, then it will return the index of the files inside the media directory. Since, show_indexes is False by default, it raises 404.
To set show_indexes to True, you can do this:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT, show_indexes=True)
This won't raise the 404 and will display a list of files inside media dir.
P.S.: I don't think displaying file indices is a good idea, unless, of course, that's something you really want. It puts server under extra load to generate the indices. And someone can download the files recursively using a program like wget etc. Even those files that are meant to be private.

Django Models.filefiled file dosent exist

I wish to access the file i upload using models.FileField(), but its shows different path when i click on the link provided in the admin page. May i know what's the problem here?
Updated my code, but the url dosent seem corret. getting a 404 code error.
you need to declare MEDIA_ROOT = os.path.join(BASE_DIR,'media')
and add them to the urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

URLConf that works on development server but not on GAE

I am using Django non-rel in Google App Engine, and my URLConf seems not to work and generate a 500 Server Error.
This is my urls.py file at the root of my app :
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^home/', include('appname.home.urls')),
)
And this is my urls.py in a subpackage home of appname:
from django.conf.urls import patterns, include, url
urlpatterns = patterns(r'appname.home.views',
url(r'^0/', 'home'),
)
It works great in development server but it does not work on Google App Engine.
I already read a related question and its answer but it did not solve my problem.
What does the error message say in the logs? You urlpatterns is malformed. The first r'' is incorrect. That should be a string. And, is the 0 in your url intentional? Change to:
urlpatterns = patterns('appname.home.views',
url(r'^$', 'home'), # matches mysite.com
url(r'^0/$', 'home'), # matches mysite.com/0/
)

Django Page not found (404)

I'm designing a photo app .
I get this error every time I view an uploaded picture on my admin page.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/images/California_Poppy.jpg
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
^cool/
^forum/
^register/
The current URL, media/images/California_Poppy.jpg, didn't match any of these.
My current settings are :
MEDIA_ROOT = 'C:/djcode/mysite/photo'
MEDIA_URL = 'http://127.0.0.1:8000/media/'
I reckon the problem is with these settings. I'm using window btw
The Django docs have a solution for you to serve media in development. Usually in production you alias your media directory to be served directly from your web server to be more efficient. To serve in development, the docs show two different solutions. You can check out the provided link to read the docs and figure out which one would be better for you.
from django.conf import settings
# ... the rest of your URLconf goes here ...
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
OR
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)