Python django LRS could not set up. File not found error - python

I am using ADL LRS to setup a LRS(Learning Record Store) system for my own use. It uses TIN CAN API. I am using ubuntu server
As the documentation states, For the setup of LRS, I need to install django adn set it up for LRS. The adl_lrs folder inside the ADL_LRS contains the setting file for django(settings.py). I am bit new to django, so I can not fully understand the meaning of this part of the file-
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = '/var/www/adllrs/media/'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://my-site-name.com/media/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
It states-
1. MEDIA_ROOT = '/var/www/adllrs/media/' which I assume it means is to put the media files like songs and videos, at this location
2. STATIC_ROOT = '' which I assumes means the path of the static directory which contains the HTML, CSS, js files.
On cloning the git, I setup the LRS, which by the way started but all the CSS broken. I looked into DOM inspector, where the link of CSS files are like-
http://my-site-name.com:8000/static/admin/css/base.css
When I visited the above url to see what's happening, I got following output as HTML(same as I get when visiting homepage, i.e http://my-site-name.com:8000)-
Page not found (404)
Request Method: GET
Request URL: http://my-site-name.com:8000/
Using the URLconf defined in adl_lrs.urls, Django tried these URL patterns, in this order:
^XAPI/
^xapi/
^admin/
The current URL, , didn't match any of these.
My urls.py looks like-
url(r'^XAPI/', include('lrs.urls')),
url(r'^xapi/', include('lrs.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))
Obviously I am not mentioning my home page in the urls.py which point to the error. So where should I put the CSS,JS files to get the broken CSS fixed and make a default home page for this? and also I have tried to send tin can statements from wordpress, but I could not get the statements on my server. can anyone tell me how to setup a proper ADL LRS on ubuntu.
PS- Do not tell me to read the documentation as I have done it like dozen times. Tell me where I am wrong in implementing the documentation.

It sounds like you have the LRS running with just gunicorn. gunicorn by itself doesn't serve static files like the JS and CSS. I found another SO page talking about serving static files with gunicorn: https://stackoverflow.com/a/12801140/1187723
As for the LRS, when I am developing and testing locally I run Django's test server, which will deliver static files. From the project's root directory ADL_LRS, start it with python manage.py runserver. https://docs.djangoproject.com/en/1.4/ref/django-admin/#runserver-port-or-address-port
When we deploy the LRS at https://lrs.adlnet.gov/xapi/ we use nginx, which is configured to serve the static files. We have some initial set up stuff about it at https://github.com/adlnet/ADL_LRS/wiki/Using-Nginx-for-Production

Related

Django FileBrowser 400 Error

I'm getting a 400 error when trying to access the /admin/filebrowser/browse/ page. I followed the instructions as per https://django-filebrowser.readthedocs.org/en/3.5.2/quickstart.html and have my URLs and installed apps configured correctly.
What I'm not too sure about are the media paths in settings.py;
FILEBROWSER_DIRECTORY = os.path.join(BASE_DIR, '/ogencat/MEDIA/uploads')
FILEBROWSER_MEDIA_ROOT = os.path.join(BASE_DIR, '/ogencat/MEDIA')
FILEBROWSER_MEDIA_URL = '/MEDIA/'
I have folder in my workspace called MEDIA and a folder within called uploads.
I wasn't too sure about what the docs wanted me to do in terms of setting these paths - I hadn't seen the getattr(settings, "FILEBROWSER_MEDIA_ROOT", settings.MEDIA_ROOT) syntax before so I just added the paths as I have done for the rest of settings.py
Thanks!
You need to add trailing slashes
Directories must exist prior accessing them

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}}" />

Unable to upload Images in Python

models
image_url = models.ImageField(upload_to="uploads/shows",blank=True,null=True)
I need to upload the image at a particular directory. I am trying to add these media files using admin panel. How do I upload the images ?
Edit 1
http://192.168.2.9:8000/static/uploads/shows/myImage.jpg
Page not found (404)
Request Method: GET
Request URL: http://192.168.2.9:8000/static/uploads/shows/myImage.jpg
'uploads/shows/myImage.jpg' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Edit 2
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
The error does not say your upload failed ; it's saying it failed to find the file once uploaded. So like the doc explains about "Serving uploaded content", you also need to set MEDIA settings like they suggest to us.
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)
and then you will be able to upload your file too

Why can't Django find my admin media files once I leave the built-in runserver?

When I was using the built-in simple server, everything is OK, the admin interface is beautiful:
python manage.py runserver
However, when I try to serve my application using a wsgi server with django.core.handlers.wsgi.WSGIHandler, Django seems to forget where the admin media files is, and the admin page is not styled at all:
gunicorn_django
How did this happen?
When I look into the source code of Django, I find out the reason.
Somewhere in the django.core.management.commands.runserver module, a WSGIHandler object is
wrapped inside an AdminMediaHandler.
According to the document, AdminMediaHandler is a
WSGI middleware that intercepts calls
to the admin media directory, as
defined by the ADMIN_MEDIA_PREFIX setting, and serves those images.
Use this ONLY LOCALLY, for development! This hasn't been tested
for
security and is not super efficient.
And that's why the admin media files can only be found automatically when I was using the test server.
Now I just go ahead and set up the admin media url mapping manually :)
Django by default doesn't serve the media files since it usually is better to serve these static files on another server (for performance etc.). So, when deploying your application you have to make sure you setup another server (or virtual server) which serves the media (including the admin media). You can find the admin media in django/contrib/admin/media. You should setup your MEDIA_URL and ADMIN_MEDIA_URL so that they point to the media files. See also http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files.
I've run into this problem too (because I do some development against gunicorn), and here's how to remove the admin-media magic and serve admin media like any other media through urls.py:
import os
import django
...
admin_media_url = settings.ADMIN_MEDIA_PREFIX.lstrip('/') + '(?P<path>.*)$'
admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^' + admin_media_url , 'django.views.static.serve', {
'document_root': admin_media_path,
}, name='admin-media'),
...
)
Also: http://djangosnippets.org/snippets/2547/
And, of course, #include <production_disclaimer.h>.

Where to put a Django template's dependent files?

My Django templates use a lot of related stuff: images, style sheets, etc.
Where should I put these file, or how should I refer to them in the template itself?
For now I'm using the development server.
I know it's a really common thing, but I can't really figure it out.
I put them inside a folder named static, which is in the web project's top level folder.
Example:
/static/img/
/static/js/
/static/css/
/templates/
urls.py
settings.py
I then have the following rule in my urls.py file:
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
My settings.py contains:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static').replace('\\', '/')
ADMIN_MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static/admin').replace('\\', '/')
Maybe you can read the doc http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files
We put ours under /media. Everything that is specifically tied to the layout of the sites is further separated. Of course none of this static content is served by Django on the production site. They often aren't even on the same physical server.
/media
/images - this is for content-specific images
/video - these next 2 are normally symlinks to a /big_content folder ...
/audio - so that they aren't included in our mercurial repository.
/layout - everything that is tied to the basic templates.
/css
/js
/images

Categories