Django - uploaded images can not be retrieved - django

I am exploring file/images uploads in Django. I have a model with Photo class which has thumb ImageField. I uploaded thumb with admin panel and I made template which just gives <img src='{{photo.thumb.url}}' />. Unfortunately I don't get any images and when I try to get direct link it says "page not found". It seems that django either does not move them to media_url or maybe it has no permission to access it? I wonder what can I do to fix it.
Thanks a lot beforehand.
PS: Running django on dev server with sqlite db.

The development server by default doesn't serve static files (see the documentation). You'll need to add the following code to the bottom of your urls.py to enable static file serving:
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'%s(?P<path>.*)' % settings.MEDIA_URL[1:], 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
That should sort you out.

Related

Django Displaying images uploaded by ImageField properly

I'm building my own porfolio using Django, and I just had a question regarding uploading images using the ImageField.
I uploaded an image through the admin page using ImageField, and after a long search session, finally got my page to display the image successfully.
urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',include('pages.urls')), # main landing page
path('admin/', admin.site.urls),
path('project/',include("projects.urls"))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
projects.html
<img src="{{project.image.url}}">
However, the official django docs say that this is not a good way to deploy a django site. Why is that, and what is the best/proper way to display images?
The reason is also given in documentation:
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
Proper way of deploying media files in production is to use a reverse proxy server like NGINX or Apache, or you can use a S3 compatible storage(like Amazon S3) to serve those media files as well. I can give you an example configuration in NGINX for serving media files:
location /media {
alias /path/to/media/direcoty; # Change to your own media directory here.
access_log off;
}

Uploading files POST urls raise 404 when deploy

I'm deploying a django app on a host with apache and cPanel. I think I did all the steps that django needs to deploy. Everything works fine except dome admin posts urls.
When I'm sending a form via post from the admin site, and one of its fields is a File that will be uploaded to a directory, the server responses me 404. Some info:
Python 3.5, Django 1.11.9
Error: 404 Not Found
When: sending any post form containing a Choose File field, even if the file isn't mandatory.
The forms without files in their fields work fine. In production everything works perfect.
I have a symlink in the public_html folder to my media and static folders.
This error only shows in the admin page. I can upload file from the site without any problem
This is my code:
urls.py
urlpatterns = i18n_patterns(
url(r'^admin/', admin.site.urls),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^', include('myapp.urls')),
prefix_default_language=False)
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
This is my first time hosting a website, so, sorry if I'm asking a dummy question, but I couldn't find any solution, even here. Also, sorry for my English. Thanks for the future answers
EDIT
My non-admin forms work perfect. The problem is in the admin page. I edited my old question with the changes in bold
It can be web server problem. Check your cPanel settings
May be something wrong with your wsgi file location
Check up some tutorial for more info.
In case you are serving media files using django's static file server, the urlpatterns variable in your project's base urls.py file should be assigned in a specific order so that the i18n_patterns(...) assignment places before the static(...) url.
you should do it like so:
urlpatterns = i18n_patterns(
url(r'^admin/', admin.site.urls),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^', include('myapp.urls')),
prefix_default_language=False)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

How can I fix my Wagtail URL namespace and explorer when adding wagtail to a current project?

My issue is my URL is coming up mysite.com/test-news-1/ instead of mysite.com/news/test-news-1/
I've started a new project using cookiecutter-django. I then wanted to add wagtail cms to the project and followed the docs to do so.
I get the wagtail admin page up fine, at /cms/ instead of /admin/ like it says to do on this page - http://docs.wagtail.io/en/v1.3.1/getting_started/integrating_into_django.html
I added a few apps just to practice and get used to wagtail, one directly copied from the wagtail blog example. This is where my issue starts.
The wagtail admin Explorer does not list my apps like it shows on the wagtail site https://wagtail.io/features/explorer/, instead it just says "Welcome to your new Wagtail site!" When I select Add Child Page it allows me to select the app pages I have set up and seems to go by my models just fine. But when I post something and click go to live site it comes up as mysite.com/blog1/ instead of mysite.com/blog/blog1/
I believe my problem that I dont understand the final part of the doc page that I linked above. It says,
Note that there’s one small difference when not using the Wagtail
project template: Wagtail creates an initial homepage of the basic
type Page, which does not include any content fields beyond the title.
You’ll probably want to replace this with your own HomePage class -
when you do so, ensure that you set up a site record (under Settings /
Sites in the Wagtail admin) to point to the new homepage.
I tried adding the homepage model from the doc page, but this didn't seem to help at all.
I'm very inexperienced, this is my urls.py file, if you need to see other files please let me know.
urls.py
from __future__ import unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name="about"),
# Django Admin, use {% url 'admin:index' %}
url(settings.ADMIN_URL, include(admin.site.urls)),
# User management
url(r'^users/', include("contestchampion.users.urls", namespace="users")),
url(r'^accounts/', include('allauth.urls')),
# Your stuff: custom urls includes go here
# Wagtail cms
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
url(r'^400/$', default_views.bad_request, kwargs={'exception': Exception("Bad Request!")}),
url(r'^403/$', default_views.permission_denied, kwargs={'exception': Exception("Permission Denied")}),
url(r'^404/$', default_views.page_not_found, kwargs={'exception': Exception("Page not Found")}),
url(r'^500/$', default_views.server_error),
]
These two url confs are in conflict =
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'', include(wagtail_urls)),
One must change, otherwise Django will always resolve the base url of `yourdomain.com/' to the first entry. One easy way to fix this is to update the second-
url(r'^content/', include(wagtail_urls)),
Now your Wagtail root page will be accessible at yourdomain.com/content.
As for mysite.com/blog/blog1/ not coming up, that Url would assume that, from your Wagtail root page, there's a page w/ slug 'blog', and then a child page of that with slug blog1. The tree structure of your Wagtail site determines the URLs by default. If you want to override that you'll have to use the RoutablePageMixin as described here.

My DJango app is responding to /static urls

Update
I figured out what was causing the stylesheets to become invisible, though I don't quite understand it all. I set DEBUG=False in settings.py to test the error handling as described in the tutorial. Somehow setting debug to false causes the static files not to be locatable. I will look further in the configs to see if I can get a clear understanding why. Until then, please feel free to answer or comment with additional info. I am still learning!
Update
I'm going through a DJango tutorial from here and I hit a roadblock. I'm up to tutorial 3 where they explain how to refactor your urls.py file when I try loading up the admin site to make sure I haven't broken it. Sure enough it looked all wierd because it was missing the stylesheets. Stylesheets are pulled from here:
http://127.0.0.1:8000/static/admin/css/base.css
When I hit that link in my browser I get the custom 404 page I configured for my app. The stylesheets were working prior but I'm not sure which change broken them. I went through my urls.py file and reverted all of the polls specific url configs to no avail. Here's my current urls.py under hellodjango (the name of my project.)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.http import HttpResponse
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
def page_not_found(request, template_name='404.html'):
return HttpResponse("Could not find the resource you asked for...")
handler404 = 'hellodjango.urls.page_not_found'
and here's the urls.py under my polls directory:
from django.conf.urls import patterns, url
# Uncomment the next two lines to enable the admin:
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Help?
It looks like you don't have a URL pattern for /static. As such, the static/admin/css/base.css URL doesn't match any pattern, and so you get a 404. Try something like this:
from django.conf.urls.static import static
# ...
urlpatterns = patterns('',
# ...
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
# ...
This should work for you -- go to /static/foo.css, and you should see your CSS.
It's worth noting that this is discouraged in a production environment. For your tutorial app, though, it'll work.
The staticfiles app provides a custom runserver management command that automatically serves the static files, are you sure you have the following in your settings?
INSTALLED_APPS = (
# ...
'django.contrib.staticfiles',
)
In production, you'll use the collectstatic management command that finds all of the static media and dumps it into STATIC_ROOT (this is the only purpose for this setting - it isn't used or needed during development).
Glad you figured it out. Here's why it works like this.
django.contrib.staticfiles overrides the runserver management command so that the static files are served automatically. To remind people that they shouldn't be using django to serve static files, this only happens when DEBUG = True, as you found out.
The documentation of the overridden management command explains that you can use the --insecure flag to make this work no matter the state of the DEBUG setting.

Django admin view uploaded photo

I have implemented photo upload in Django but I have a problem to view it Django admin.
models.py
class WorkPlacePhoto(models.Model):
file = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='uploads')
Photos are saved in PATH_TO_APP/media/uploads/ and I can view them in my app. However, in admin panel, when I clicked on the link which admin app is generated it gives me 404 error as
Request Method: GET
Request URL: http://127.0.0.1/admin/wpphotos/workplacephoto/9/media/uploads/aosa_glb_ho_778_hi.jpg/
u'9/media/uploads/aosa_glb_ho_778_hi.jpg' object is not found
Although the message is clear, I couldn't figure out which url should be written to view the image and of course how admin class should be modified.
I glad to suggest me a way to achieve this. Thanks...
From you description I am guessing your MEDIA_URL isn't set correctly, something which is a bit tricky to do using the Django development web server.
I am guessing that the link's href would probably be media/uploads/aosa_glb_ho_778_hi.jpg where you probably want /media/uploads/aosa_glb_ho_778_hi.jpg so it is relative to http://127.0.0.1/ not to where you happen to be now http://127.0.0.1/admin/wpphotos/workplacephoto/9/.
See the static files documentation for inspiration of how to serve your images with the Django development server.
I have the same issue but I managed to fix it -but it's not a correct method and I'd like to use a better solution if possible.
What I did was, I have apache running on port 80, so I created a symbolic link in the /var/www folder which is pointing to the Images folder in my Django App directory. And the Media URL is basically this:
MEDIA_URL = 'http://127.0.0.1/Images/'
This work fine, but I don't like the solution.
I didn't quite follow the solution explained above. Could someone please explain more?
By now, you probably either solved your problem or quit it altogether, but after all these years people (like me) still have this problem. Here's how I just solved it:
On settings.py, I set not only STATIC_URL (with the name of the static folder) and STATIC_URL (with the path to that folder) but I also set MEDIA_URL and MEDIA_ROOT accordingly, where the uploaded images will be stored at;
On my model class, I have my picture field looking as follow:
picture = models.FileField(upload_to='images/')
It will upload the image to my MEDIA_ROOT folder, appending /images/ to it.
And finally, on my urls.py, my urlpatterns looks like this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Notice how I appended the static right after the list.
Django's documentation makes it clear that this strategy is not recommended for production environment and it gives some alternatives you can choose from. For more information: https://docs.djangoproject.com/en/1.10/howto/static-files/#serving-files-uploaded-by-a-user-during-development
You probably have in your settings.py
MEDIA_URL = 'media/'
Being the url relative, you have that behavior in the admin panel. The solution would be to set the url as absolute
MEDIA_URL = '/media/'