Installing TinyMCE on Django (And using it on forms) - django

Hi Im trying to install TinyMCE on a Django project and Im totally lost about static files, MEDIA, and the world itself.
I want to use TinyMCE in one of the fields of a form:
class MovieForm(forms.ModelForm):
class Meta:
model = Movie
fields = ['title', 'language', 'description']
widgets = {
'languages': forms.SelectMultiple(),
'description': TinyMCE({'cols':80, 'rows':30}),
}
I installed django-tinymce
pip install django-tinymce
Then I added it to the installed apps
INSTALLED_APPS = (
...
'tinymce',
...
)
And then added the urls in my project urls.py
urlpatterns = patterns('',
...
(r'^tinymce/', include('tinymce.urls')),
...
)
Great. So what do I do next?
I read the Configuration part on http://django-tinymce.readthedocs.io/en/latest/installation.html#configuration but I dont get it.
Should I add TINYMCE_JS_URL = os.path.join(MEDIA_URL, "path/to/tiny_mce/tiny_mce.js") to my project settings.py? Where do I put tiny_mce.js? Should I configure MEDIA_URL somewhere?
Would be awesome if someone can point me in the right direction.
Thanks! :)

I figured this, so I'm posting an answer in case anyone else comes across this.
I read more in details the documentation on Static files (https://docs.djangoproject.com/en/1.10/howto/static-files/), and read the related section of the great book Tango with Django (http://www.tangowithdjango.com/).
That helped me to understand the MEDIA and STATIC setup I needed to get right in order to get working TinyMCE with Django.
tiny_mce.js went to the static files folder (specifically to static/tiny_mce/).
The settings.py of Tinymce check if static is confidured, and points there to get the needed files. Tadaa! It works!
Hopefully it will help someone!

Related

Why is the Django-Simple-Captcha image not showing up?

I'm trying to add a Django-Simple-Captcha image to my application's login screen.
This is what I have added at the top of my forms.py file:
from captcha.fields import CaptchaField
This is what I have added to the registration form:
captcha = CaptchaField(
label="What does this say?",
required=True,
)
This is what I added to my site's url.py file:
urlpatterns += patterns(
'',
url(r'^captcha/', include('captcha.urls')),
)
I have also added "captcha" to my INSTALLED_APPS in settings.py
However, when I load the page, I see that the Captcha image is a broken link: http://predictstat.com/accounts/register/. The server shows this on the console:
[23/Dec/2013 16:30:47] "GET /captcha/image/56edd656ba57a2a3e71571373e1a59c564e3d592/ HTTP/1.1" 500 72336
However, there is no such directory "captcha" under the directory for my application. So where is it trying to look for this image? And why doesn't it exist?
On closer inspection, I found the issue was this:
Exception Value: The _imagingft C module is not installed.
Details how to solve this issue can be found here: Python: The _imagingft C module is not installed
There is no need to create any captcha directories.
The problem is that you did not update your urls.py as mensioned in the documentation:
urlpatterns += patterns('',
url(r'^captcha/', include('captcha.urls')),
)
Another problem could be that you did not run syncdb:
./manage.py syncdb
./manage.py migrate # If you use migrations
The same was happening in my application and I manage to make a very strange workaround (XGH alert) usising two slashes in django-simple-captcha 0.4.4
urlpatterns += patterns('',
url(r'^captcha//', include('captcha.urls')),
)
I think it may has something with the conflict of globally and virtureenv environment,may be there is another app which contains the name captcha.

sorl-thumbnail doesn't generate placeholder images

I'm trying to upgrade a django project using the old sorl-thumbnail (v.3.2.5) to the newest (v.12.0) but I'm not able to get it generate placeholder images in development environment using the settings provided: http://sorl-thumbnail.readthedocs.org/en/latest/reference/settings.html#thumbnail-dummy
Here are my settings:
THUMBNAIL_DEBUG = True
THUMBNAIL_DUMMY = True
THUMBNAIL_DUMMY_SOURCE = 'http://placekitten.com/%(width)s/%(height)s'
MEDIA_URL = '/media/'
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
This is the model using sorl ImageField:
from sorl.thumbnail import ImageField
class Cover(models.Model):
[... other fields here]
image = ImageField("immagine", upload_to='images/cover/%Y/%m/%d', max_length=255)
and the admin inherits from sorl.thumbnail.admin.AdminImageMixin.
The project uses Django 1.6 but I tried same settings on another project which uses Django 1.5.5 and I have the same problem.
Thumbnails are correctly generated (and retrieved from cache) for newly updated images, but pre-existent images are not substituted with placeholders, neither in admin nor in frontend pages (development server answers with a 404).
Any clues? Sorl docs are really scarce...
After searching through sorl-thumbnail code I found out that in the admin the THUMBNAIL_DUMMY setting is not even considered...
There is a pull request to solve this (opened a year ago): https://github.com/mariocesar/sorl-thumbnail/pull/128
As for the frontend it works, it was just a silly mistake in the template.

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/'

Should I remove 'django.contrib.comments' from my installed apps when I modify it by subclassing?

I'm customizing django comments.
According to the docs you'll need to add your customized app to INSTALLED_APPS in settings.py, and set COMMENTS_APP to your app name.
INSTALLED_APPS = [
...
'my_comment_app',
...
]
COMMENTS_APP = 'my_comment_app'
Should I also remove 'django.contrib.comments' from INSTALLED_APPS?
If you are only extending contrib.comments not replacing it, you shouldn't remove it from installed apps since, for example, most of the templatetags you need are in that application.
In order for Django to find the templates, templatetags and so on app must be in the installed apps.

Registered models do not show up in admin

I added a model to admin via admin.site.register, and it does not show up in admin. Since admin is so "It just works", I have no idea of how to debug this. Pointers?
After adding and registering your admin:
# app/admin.py
class YourModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
Make sure your app is in your project settings.py:
# settings.py
INSTALLED_APPS = (
# other apps ...
'app',
)
Sync your project for that model if you have not done so already:
python manage.py syncdb
Restart your server, CTRL-C:
python manage.py runserver
In such a situation is also a good practice to check if the user logged in to the admin panel has rights to manage such a model. If they do then you could change your code to access the functions as root.
When in doubt, shut down server, syncdb, start server.
I have the experience, that sometimes after changing an admin.py the dev-sever won't be restarted. in that case touch settings.py helps.
I think the checklist in Thierry's answer is almost definitive, but make sure that urls.py contains admin.autodiscover() to load INSTALLED_APPS admin.py modules.
# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)
More info in the django docs.
Have you added the application to your installed apps? That has happened to me both one and two times. :) Otherwise it would be useful for us to see the code to help you.
Also make sure there are no syntax errors in your admin.py or anything. That can cause an app to fail to be registered with the AdminSite.
I've faced the same problem, but it was a little tricky than yours.
Consider, that you have a project with, say, five or even more apps. For me it is more obvious to register all models in just one admin.py file, so I have decided to do it in one place - core directory. Of course, it was not an app, so none of models showed up on admin page.
comment out the some lines in urls.py see docs for more details
admin.autodiscover()
urlpatterns = patterns('',
('^admin/', include(admin.site.urls)),
)