Django Admin Page not found when including MEDIA_ROOT setting - python

this is my first question so please point me to the right direction in case I miss something of importance ☺️
So my problem is the following:
I'm currently creating a website with Django, which is supposed to list a variety of projects from the database. Each project has a picture associated with it. The path to this picture is saved in the database (each picture is added as models.ImageField() to the projects model). In order to serve them, I added MEDIA_ROOT = os.path.join(BASE_DIR, 'main/media') to my settings.py and added the following snippet to my urlpatternsin urls.py: + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now to the crux of it: Everything works fine, except I can't open my admin panel now. The media files are served as expected in the projects overview, but at soon as I try to open the /admin page, I get following error message:
Page not found (404)
“/Users/.../main/media/admin” does not exist
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Raised by: django.views.static.serve
as soon as I remove + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) from my url patterns, the admin page works fine again, however the media files are not served anymore.
Can someone show me what I've broken here?
Thank you very much!

I had the same problem. I had a link on my site, that when pressed took me to admin panel.
The mistake was that I was missing a trailing slash.
This did not work:
Admin
This worked:
Admin

You might not have added the MEDIA_URL path in setting.py.
In your case:- add this to settings.py:- MEDIA_URL = "/main/media/". If it doesn't work, try removing one of the two corner slashes. Hope it will work.

Related

FilePathField for Image Path not working with Django Admin Panel

New to Django and web development in general. But I have followed a tutorial for creating my first site. Everything worked fine, until I finished it and wanted to edit the info using the Django admin panel.
The main issue is that I have a model shown below that uses FilePathField to describe the image locations. The tutorial had me place the images in /home/pi/mysite/projects/static/img.
from django.db import models
class Project(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
technology = models.CharField(max_length=20)
image = models.FilePathField('/img')
def __str__(self):
return self.title #or self.title for blog posts
This worked fine at first for displaying the images on the website, but once I tried to navigate to the /change/ url from the admin panel, I would get FileNotFoundError at /admin/projects/project/1/change/ [Errno 2] No such file or directory: '/img'
So I looked around and found that FilePathField is an absolute path, so tried defining it absolutely with /home/pi/mysite/projects/static/img/. This stopped the error from occurring which let me at least access the /change/ url from the admin panel. The panel now showed the image field with a dropdown that I could use to choose between the different images I had up there, which were located at /home/pi/mysite/projects/static/img/. But from the admin panel, once I hit save, the images would no longer show up on the site. Upon using inspect element on the website, it seems it was trying to pull the images from /static/projects/static/img. I then tried to move the images to /static/projects/static/img but then they no longer showed up in the dropdown box from the admin panel, and also weren't displaying on the website.
So at this point I am just a bit confused where I should be pointing FilePathField too that will allow me to display the images on the website and also change them through the admin panel.
A much better approach is to use django's FileField like so:
image = models.FileField(null=True, blank=True)
In your settings.py you need to include the media path. Check you have this in your settings.
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
Last step is to extend the urlpatterns in your urls.py:
urlpatterns = [
# your paths
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will generate a /media/ directory where your uploaded media files will point to. Then in your frontend you can simply use the model property image.url to get the absolute url of the image.

How to change the storage default location in Scorm xblock

I am using hawthorn version open edX, Also using scorm xblock https://github.com/overhangio/openedx-scorm-xblock for upload the scorm. by default, it is using /edx/var/edxapp/media/scorm/ path. I want to change the storage location.
For this, I have tried two solutions.
First solution:-
In the lms.env.json and cms.env.json change the path.
MEDIA_ROOT = /my/new/path/media/
MEDIA_URL = /media/
Also, added below the line in url.py
urlpatterns = patterns('',# ... the rest of your URLconf goes here ...) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
It is uploaded successfully in this path /my/new/path/media/, but when I try to get in the LMS, it is showing a 404 error.
Second solution:-
As par scorm xblock, We can change to default location by give the path in below code.
XBLOCK_SETTINGS["ScormXBlock"] = { "LOCATION": "/my/new/path/media/",}
I have added the code in comman.py and scormxblock.py, But it is not working.
Or, Please correct me if I am adding the code in the wrong place.

Correct way to save an image in admin and showing it in a template in Django

I am using MySql and Django. I need to select the image route stored in the database, I can't use the url property of an ImageField, It must be the route stored in the database.
First of all, this is my configuration in settings:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = PROJECT_ROOT + '/archivos/'
MEDIA_URL = '/archivos/'
My first try on the model was this:
foto = models.ImageField(upload_to = settings.MEDIA_ROOT)
And the URL saved on the database was this:
/home/developer/project/mysystem/archivos/hombre.png
Later I tried this on the model:
foto = models.ImageField(upload_to = '')
And the new URL is this:
./pantalonazul_pXNLcuF.jpg
In both cases the image is uploaded to the MEDIA_ROOT folder, but when I try to use the URL saved on the database, the image doesn't shows. I am changing the src with jquery, I thougth that was the problem, but no, because I putted by hand both URL's in an image, and none of them worked. Also tried this: ../../archivos/pantalonazul_pXNLcuF.jpg but produced the same bad results.
When I enter to the admin, it shows this:
And if I click on that link I get this URL on Address bar:
http://127.0.0.1:8000/archivos/pantalonazul_pXNLcuF.jpg
I also tried to use that URL in the image, written by hand on the src property, but didn't worked.
As an extra, when I click on the link to get that last URL, I have an error that says that the URL is not registered on the urls.py file, shows me a list of my URL's and at the end says this:
The current URL, archivos/pantalonazul_pXNLcuF.jpg, didn't match any of these.
What I'm doing wrong? Why I cant get the image? Is something in the settings or where?
P.S.: The folder where I save the images is outside my app folder, is on the "general" folder, or I don't know how to call it, is in the same folder where settings.py is located, the folders are something like this:
-MyAppFolder
-MySystem
-archivos
-image1.jpg
-image2.jpg
-settings.py
-urls.py
-wsgi.py
I have found a partial solution, reading this Settings Media URL and after it redirected me to this Servinge files during development.
I'm using the last model, the one with the empty uploaded_to and just added the line that the docs recommend on the second link to the urls.py file, and also call the image on this way: ../../archivos/image.jpg but it says that the urls.py solution only works for development, not for serving it as a web page/application, so, someone knows how to make it work in a real server? without using DEBUG=true on settings?

ImageField in Django 1.5.4 -- dealing with the images' paths

I am using Django 1.5.4 and I have a project with the following structure:
django_site
-django_site
-books # the app
-media
-images
-books
-authors
-static
-images
-templates
This is the necessary code:
# settings.py
MEDIA_ROOT = '/root/django_site/books/media/'
MEDIA_URL = '/media/'
# models.py
class Book(models.Model):
image = models.ImageField(upload_to="images/books/")
# urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^books/$', 'books.views.get_all_books'),
)
My problem is that, after adding the image via the admin site, if I click on the image link below the upload button a 404 page shows up, complaining the path does not exist.
The request URL is http://127.0.0.1:8000/media/books/media/images/book/out_of_control_1.JPG but in fact I want the result to be /root/django_site/books/media/images/out_of_control_1.JPG.
How do I fix that? Looking forward to your responses.
Since you seem to use the developpement server, I think your issue is related to this one : Django MEDIA_URL and MEDIA_ROOT
As explained in the answer, you have to set up a specific URL pattern for media handling when debug is set to True.
You can also have a look at the docs regarding this question.

Django serving dynamic Files

I have a problem about serving admin uploaded files in my templates.
I set:
MEDIA_URL='/media/'
MEDIA_ROOT = 'assets/'
and my models are like:
mainPhoto = models.ImageField(upload_to="articles/")
When in my templates, I try:
<img src="{{MEDIA_URL}}{{article.mainPhoto}}" />
The image doesn't show up on the website.
I found one deprecated solution with the django .serve(), but I hope to avoid using it.
I was looking everywhere and didn't find any answer, so I hope someone here will be able to help me.
There are two parts to make this work. First is the Django configuration. There are really two settings for this which you have already mentioned - MEDIA_URL and MEDIA_ROOT. As already noted in the comments the MEDIA_ROOT has to be an absolute path. For example:
MEDIA_ROOT = `/abs/path/to/project/media/`
Second part is actually serving the files. If you are in production, you NEVER want to serve your static or media files through Django so you should configure Apache or nginx or whatever server system you are using to serve the files instead of Django. If you are on the other hand still developing the app, there is a simple way to make Django serve media files. The idea is to modify your urls.py and use the Django's static files server to serve media files as well:
# urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# other patterns
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
More about this approach at Django docs.
Also FYI, you probably should not use {{MEDIA_URL}}{{article.mainPhoto}} to get the url of an image. This approach will break for example if you will no longer use file system storage for static files and will switch to something different like Amazon S3. It is always a good idea for the storage backend to figure out the url:
<img src="{{article.mainPhoto.url}}" />

Categories