Django ckeditor upload image - python

I'm trying to use ckeditor to upload an image. Looks like everything is set up by documentation, but still I'm getting an error while trying to upload an image. I think that there is a problem with static files. Looks like ckeditor doesn't know where to upload files, even though I've provided all needed parameters:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_IMAGE_BACKEND = 'pillow'
I'm getting this message:
[23/Feb/2020 20:17:47] "POST /ckeditor/upload/&responseType=json HTTP/1.1" 302 0
And here's what I get in browser. The red one is for "incorrect server response".

Looks like there is a problem with 'static' folder location in my project. I've solved my problem adding
CKEDITOR_STORAGE_BACKEND = 'django.core.files.storage.FileSystemStorage'
To my settings file. Not sure if it will work for you, but it definitely works for me since 'FileSystemStorage' looks for 'MEDIA_ROOT' setting by default.

Related

Media files found in development but not production pythonanywhere

I have a basic file upload system written using django. It works and the files are properly uploaded to the correct path. However, when using google drive viewer it comes up as preview not available. After some digging around I figured the problem must be that the url is not being found, which it is not.
In other words mysite.pythonanywhere.com/media/files/myfile.pdf returns a 404 error.
However, http:127.0.0.1:8000/media/files/myfile.pdf works perfectly fine (on local server of course). So this is a problem with serving the files in production.
My media url is correctly mapped to the media directory, as shown on the pythonanywhere help page.
URL = /media/
DIRECTORY = /home/name/mysite/media
The files are saved in the correct place on the filesystem. My static files are served fine and have a near identical setup to the media files.
I've been trying to solve the problem for two days now but haven't found a solution. Any ideas?
In my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL ='/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
It could be worth noting that when I intentionally place the wrong path to a static file, the 404 error doesn't include a list of urls which django has searched. It does in the 404 error messages for media. So the server is directly serving the static files without passing the request down to the wsgi layer, but for some reason it's not handling media urls the same way? By the way I am working with debug=False but only set it to True to help diagnose the problem. Here are the messages:
Request Method: GET
Request URL:http://infodump.pythonanywhere.com/static/randfile.txt
'randfile.txt' could not be found
Request Method: GET
Request URL: http://infodump.pythonanywhere.com/media/randfile.txt
Using the URLconf defined in InfoDump.urls, Django tried these URL patterns,in this order:
1.^admin/
2.^streamer/
3.^ ^$ [name='home_page']
4.^ ^signUpPage/$ [name='sign_up']
The current URL, media/randfile.txt, didn't match any of these.

Django sorl-thumbnail don't store files in my app

I read several topics here about this problem, but I can't manage to resolve it. Everybody asks if the MEDIA_ROOT config is okay, and other answer "yes sure!" But I did not find a real explanation, so:
I can see the generated filename (from db) in my template (a pdf genreated by weasyprint) :
/media/ cache/92/00/9200d02288657e7accae5666d37188a1.jpg
The object.exists in the thumbnail template tags returns, without surprise, False.
The file isn't generated.
In my settings.py I have this config :
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
That worked fine with a basically FileUpload field.
I think I just don't understand the path configuration of MEDIA stuff.
Some explanations on good practices about dealing with MEDIA will be welcome !
EDIT : I uninstalled sorl-thumbnail and now use easy-thumbnails.

django ImageField not uploading the file

So i've been googling this issue for the past hour and can't come up with a solution. Basically this is it: in my model.py i have a class that has this
class Case(models.Model):
zoomOutImage = models.ImageField('Label', upload_to="zoomOutImage")
and in my settings.py i have my media URL/ROOT set up like this
MEDIA_ROOT = os.path.join(os.path.abspath(''),'app/static/ds/')
MEDIA_URL = '/static/ds/'
which from the webserver should serve out like this:
http://127.0.0.1:8000/static/ds/zoomOutImage/actinic_granuloma_3.jpg
I've installed PIL (inside virtualENV) and there are no errors in uploading, the only issue is when i try uploading the file via the admin panel nothing happens. No errors nothing. The file just simply doesn't get uploaded to the zoomOutImage folder by the development server. Can anyone point me towards why?
I guess your file is in a subdir of your root, subdir named 'zoomOutImage'. Or even a file called like that in the root. I remember putting a function call in the upload to string. That function creates a path and filename, using os.join and the filename from the instance. Doing this by head, no example code available right now. But must be able to google this.
Look here https://stackoverflow.com/questions/1190697/django-filefield-with-upload-to-determined-at-runtime
And by the way, I totally disagree with your answer, you should NEVER use absolute paths in your settings! See this answer use css in django 1.4 development for how to use the correct settings and refer to your Project PATH
EDIT (after reading your own answer)
Guess you are missing this first step:
this is the path to your settings.py file:
SETTINGS_DIR = os.path.dirname(os.path.realpath(__file__))
and than this is the path to your project dir: (I Am using buildout, so call it buildout, but it's the root of your project):
BUILDOUT_DIR = os.path.abspath(os.path.join(SETTINGS_DIR, '..'))
and from there on you can define everything you want:
STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'static')
STATIC_URL = '/static_media/'
MEDIA_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'media')
MEDIA_URL = '/media/'
and in your template file refer to the image like:
<img src="{{MEDIA_URL}}{{ case.zoomOutImage }}" width="100%">
when your object given to the template is called case
about your question of the urls.
you should add this:
if settings.DEBUG:
urlpatterns += patterns('',
(r'', include('staticfiles.urls')),
)
and see the link above to the question about using css, it's the same problem, but there for finding the css files during development. It's all about the static file places.
import os
# get abspath
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
MEDIA_ROOT = rel('media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = '' #if only your static files are in project folder
STATICFILES_DIRS = ( rel('static'),) #if only your static files are in project folder
use this settings, and everything will work
so i finally solved my problem. For anyone having this issue in the future do the following:
if you're trying to serve static media files locally on the development server use absolute paths for MEDIA_ROOT and MEDIA_URL.

Problem with serving pictures in Django

I'm trying to create my first site in django and I've run into a problem. I'm trying to serve pictures,but it isn't working correctly. I have it set up in the following way:
In settings.py:
MEDIA_ROOT = 'C:/Users/John/Documents/My Dropbox/Infostuff/filmsite/media/'
MEDIA_URL = 'localhost:8000/static/'
ADMIN_MEDIA_PREFIX = '/admin-media/'
In urls.py:
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
It looks like this in the template page:
<img src="{{MEDIA_URL}}{{a.picture.url}}">
And it is parsed to this:
<img src="localhost:8000/static/portrets/0000138_Leonardo_D.jpg">
When I open the html page it doesn't display the pictures (I get the broken picture icon). If I however go to view the source and copy the above url and past it directly into my browser it does load the picture. What am I doing wrong?
I'm using Django 1.2. I'm not using Apache yet, because I would first like to get it working in a development environment.
PS: This is the first time I'm asking a question on this site, if I did something wrong, please tell.
use:
MEDIA_URL = 'http://localhost:8000/static/'
or
MEDIA_URL = '/static/'
Your MEDIA_URL should just be '/static/' - or, if you must, 'http://localhost:8000/static/'. Otherwise your browser is interpreting the localhost as part of the path, not the domain.
Ok, I feel very stupid...
I had to change
MEDIA_URL = 'localhost:8000/static/'
to
MEDIA_URL = 'http://localhost:8000/static/'
and then I had change
<img src="{{MEDIA_URL}}{{a.picture.url}}">
to
<img src="{{a.picture.url}}">
Thank you for your time.
What version of Django are you using?
Can you post the generated HTML code? if the URL works when you copy and paste in the browser, it might be an html issue as well.
Have you looked at this page yet?
http://docs.djangoproject.com/en/dev/howto/static-files/
Update:
Can you post the model for a? is a.picture an image field? If so, then you don't need to put the MEDIA_URL in your img src, since it is already an absolute url and it should include the MEDIA_URL already. try removing that and see if it works.
<img src='{{a.picture.url}}' />
For more info see this page.
http://docs.djangoproject.com/en/1.3/ref/models/fields/#django.db.models.FileField.storage

Django "SuspiciousOperation" Error While Deleting Uploaded File

I'm developing in Django on Windows XP using the manage.py runserver command to serve files. Apache isn't involved. When I login to the administration and try to delete a file I get a "SuspiciousOperation" error.
Here's the traceback:
http://dpaste.com/123112/
Here's my full model:
http://dpaste.com/hold/123110/
How can I get rid of this "SuspiciousOperation" error?
EDIT: Here are my media settings:
MEDIA_ROOT = 'C:/Server/Projects/postnzb/static/'
MEDIA_URL = '/static/'
What is your MEDIA_ROOT in settings.py? From the back-trace, it seems you have set your MEDIA_ROOT to /static/.
This error is coming since Django is trying to access /static/ to which it has no access. Put an absolute pathname for MEDIA_ROOT like C:/Documents/static/ and give full permissions to Django to access that directory.
That should solve your problem.
Addendum: Since your MEDIA_ROOT seems to be OK, I am guessing that you are using MEDIA_URL for deleting the file instead of MEDIA_ROOT. Indeed, from the error it seems that Django was trying to access the /static/files/8.nzb and was denied access. Clearly, /static/ is your MEDIA_URL and not your MEDIA_ROOT. The model methods should never try accessing the files using the MEDIA_URL. I am sure a review of your code will spot the error.
Update: I skimmed your code and it seems you are setting File.nzb to %(1)sfiles/%(2)s.nzb' % {'1': settings.MEDIA_URL, '2': self.pk} which uses its MEDIA_URL and then in the delete() method you are calling the delete() method of the super-class of File as super(File, self).delete() which is obviously wrong as it will try deleting File.nzb and will try accessing the file through the MEDIA_URL. Fixing that will get rid of the error. I will leave the exact solution as an exercise to you :)

Categories

Resources