Issue in loading user uploaded images in Django - django

My urls.py
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'photo.views.home', name='home'),
# url(r'^photo/', include('photo.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('django.views.static',
(r'media/(?P<path>.*)$', 'serve', {'document_root': settings.MEDIA_ROOT}),
)
Settings.py
MEDIA_ROOT = 'E:/photo/media'
MEDIA_URL = '/media'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
When i try to access user uploaded images it gives me following error.
Using the URLconf defined in photo.urls, Django tried these URL patterns, in this order:
^admin/doc/
^admin/
media/(?P<path>.*)$
The current URL, images/about-landscapes-nature.jpg, didn't match any of these.
What am i doing wrong?
User uploaded images are getting stored in photo\media\images
Following is the directory structure in my Win7
photo
media
photoapp
models.py
settings.py
urls.py

I'm not sure if this'll fix all your problems, but MEDIA_URL needs a trailing slash, so it should be:
MEDIA_URL = '/media/'

Related

Static url goes after the regular page url

My static files only load on index page, but when navigating to another page like: examle_url/page, it returns a 404 error since it loads it like that: http://127.0.0.1:8000/categories/default/static/js/main.js, see the static loads after page
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '/static/base'),
)
urls.py "all my urls are in main directory and then i import views from apps"
urlpatterns = [
path('admin/', admin.site.urls),
path('profile/', profile, name="profile"),
path('', index, name="index"),
path('category/', category, name="category"),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If it matters my load static files are all in theme.html file and is then extended in all other html files.
Any help will be greatly appreciated, thanks in advance!
You should prepend the STATIC_URL setting [Django-doc] with a slash:
# settings.py
# …
# with a leading slash
STATIC_URL = '/static/'
# …
If you do not do that, then the url will look like static/js/main.js. If the path does not start with a slash, then that is relative to path the current URL, and thus if the page is /categories/default, it will look at /categories/default/static/js/main.js.

Django : TypeError: static() got an unexpected keyword argument 'document_root'

I am trying to view my uploaded image through web-browser/DRF-browsable API. So I added MEDIA_URL and MEDIA_ROOT to my setting.py file. When I trying to run the code , it showing TypeError: static() got an unexpected keyword argument 'document_root' . Here is my relevant portions of code,
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR,'Images')
MEDIA_URL = '/Images/'
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.templatetags.static import static
urlpatterns = [
# Examples:
# url(r'^$', 'project.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^$', index),
url(r'^health$', health),
url(r'^admin/', include(admin.site.urls)),
url(r'^sample/',sampelview),
url(r'myapp/',include(myRouter.urls)),
url(r'^test/$', testRequest),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
This error is due to you are imported wrong References of static.Try to use from django.conf.urls.static import static instead of from django.templatetags.static import static

MEDIA_URL path doesn't show up

I have a problem with my static and media settings, so my uploaded images doesn't show up on my site page.
In my "settings.py" I have:
MEDIA_ROOT = (
os.path.join(BASE_DIR, '..', 'static', 'media')
)
MEDIA_URL = '/media/'
STATIC_ROOT = (
os.path.join(BASE_DIR, '..', 'static'),
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '..', 'static'),
)
In my "models.py" I have:
expert_photo = models.ImageField(upload_to='profiles')
Some "urls.py":
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^experts/all/$', 'expert.views.experts', name='experts'),
url(r'^experts/get/(?P<expert_id>\d+)/$', 'expert.views.expert', name='expert'),
)
And after all of that, when I go to my page, I see, that the picture have link, like this:
http://127.0.0.1:8000/static/profiles/i_vagin.jpg
But it must be:
http://127.0.0.1:8000/static/media/profiles/i_vagin.jpg
So how can I fix that?
Media files are not, by default, served during development. To enable media file serving, you need to add the following code in your urls.py file:
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)
Source: Django docs
Update
You'll also need to access images in your templates like this: {{ expert_photo.url }}

Django: updating file

I'm trying to upload a file but once it's uploaded I'm not able to open it since it shows an error saying that there's no an object with that Primary key. I think the error(s) should be in urls or in the path (MEDIA_ROOT) but can't find out what is wrong.
Any suggestion?
setings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "media/"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")
]
models.py
class Foo(models.Model):
file = models.FileField(upload_to="file_folder/", blank = True, null = True)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
--EDIT--
I'm using the Admin interface so there's no Views.py
And the traceback is not shown, only the following error:
ERROR
Request Method: GET
Request URL:http://localhost:8000/admin/db_personal/foo/3/media/file_folder/django.pdf/
foo object with primary key u'3/media/file_folder/django.pdf' does not exist.
MEDIA_URL needs to be an absolute path, like STATIC_URL:
MEDIA_URL = "/media/"

django static files weird behaviour

in my settings.py I have:
import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
# ... #
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
# ... #
STATIC_URL = '/static/'
in my urls.py I have:
urlpatterns = patterns('',
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
and in my template:
<img src="{{ STATIC_URL }}images/myimage.png">
Which doesn't work. The weird thing is that if I change my settings.py:
STATIC_URL = 'static/'
and my template to:
<img src="/{{ STATIC_URL }}images/myimage.png">
does work!! I have been chasing round in circle to fix this, and I have looked at lots of forum posts, but I can't seem to figure out what I'm doing wrong.
if you use staticfiles app you don't need this (assuming you are using Django 1.3):
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
})
Its handled by the app automatically. Try to remove and see what happens.