Django Tinymce js not loading - python

hi all
Im tryng to integrate a tinymce in a django admin page.
I've installed the django-tinymce module (http://code.google.com/p/django-tinymce/)
I followed the instructions so these are my files:
settings.py
INSTALLED_APPS = (
...
'tinymce',
)
TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,paste,searchreplace",
'theme': "advanced",
}
TINYMCE_SPELLCHECKER = False
TINYMCE_COMPRESSOR = False
url.py
urlpatterns = patterns('',
# Example:
# (r'^uboPy/', include('uboPy.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
)
i have the tinymce's js in a folder in the root called media/js
In the model i've this line: text = tinymce_models.HTMLField()
when i run the server i don't get error but when i go in the admin area of my model the tinymce is not loaded. With firebug i see that the tinymce library give a 404 error but the path is correct.. i have some problem in my url.py?
thanks for the help

Are you just putting a folder in your project root called media/js? It actually needs to be served somehow from a url somewhere.
The JavaScript file is loaded via your browser, so it needs to be accessible to the internet where all of your other media lives. This part has nothing to do with django, so you shouldn't expect errors or URL issues.
Once you've solved that part, specify the URL where the file can be accessed via the TINYMCE_JS_URL settings parameter.
For example, on my setup, the js is..
TINYMCE_JS_URL = '/media/js/tiny_mce/tiny_mce.js'
With firebug i see that the tinymce
library give a 404 error but the path
is correct..
Are you saying you can visit this URL path and the JS file loads? What I'm saying is: how is the path correct if it's a 404?

I found out the django-tinymce documentation is outdated, i.e. partially wrong.
What I discovered is that different versions of tinymce and django-tinymce packages are not compatible.
I solved it adding some variables to my project/settings.py and altering the tinymce directory and file names.
django-tinymce urls.py had some hardcoded paths in it which assumed the directories were named "tiny_mce" when in reality they were named "tinymce", hcen I had to rename them, or change the django-tinymce urls.py.
# project setting.py
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_JS_DIR = os.path.join(STATIC_DIR, "js")
TINYMCE_JS_ROOT = os.path.join(STATIC_JS_DIR, "tiny_mce")
TINYMCE_JS_URL = os.path.join(TINYMCE_JS_ROOT, "tiny_mce.js")
#TINYMCE_JS_ROOT = os.path.join(STATIC_JS_DIR, "tiny_mce")
#TINYMCE_JS_URL = os.path.join(TINYMCE_JS_ROOT, "tiny_mce.js")

Related

Missing leading slash on images uploaded with Django Filebrowser

I'm having an issue with Django-Filebrowser when I try to upload an image. I'm using the following versions: Django 1.8.7, Filebrowser 3.6.4 and Python 2.7.
When I try to upload an image, everything works as expected, and the uploaded files are placed in the right folder without any problem; but the resultant file url doesn't have any leading slash necessary to serve properly the file.
For example, if I try to upload "test.jpg", the final image url will be "uploads/test.jpg" and "_versions/test_xxx.jpg" that actually creates issues even in the Filebrowser panel, since the resulting request will be issued to http://localhost:8000/admin/filebrowser/browse/uploads/test.jpg and http://localhost:8000/admin/filebrowser/browse/_versions/test.jpg rather than http://localhost:8000/uploads/test.jpg and http://localhost:8000/_versions/test.jpg that the server would serve properly.
(Note that the Filebrowser panel url is http://localhost:8000/admin/filebrowser/)
Here's my settings:
urls.py:
urlpatterns = [
url(r'^admin/filebrowser/', include(site.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('pages.urls')),
url(r'^blog/', include('articles.urls')),
url(r'^tinymce/', include('tinymce.urls')),
url(r'^grappelli/', include('grappelli.urls')),
]
settings.py
FILEBROWSER_DIRECTORY = 'uploads/'
FILEBROWSER_VERSIONS_BASEDIR = '_versions/'
Add a leading slash to the FILEBROWSER_DIRECTORY would trigger a SouspiciusFileOperation error since '/uploads/' is located outside the project folder.
Thanks in advance.
Finally I've got the point. It's enough to set explicitly MEDIA_ROOT and MEDIA_URL:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Now Filebrowser will search for "uploads/" and "_versions/" folders in this new parent one, so "/media" needs to be served accordingly.

Django display user uploaded content

I have yet to wrap my head around django and URLs, and my confusion is now preventing me from doing what I feel like should be a very simple task.
I have successfully implemented file upload.
In my settings.py file, I have added the specifications for where to store the uploaded files and the URL Django should use to serve them.
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL= '/media/'
I also added the necessary line to urls.py to allow Django to serve files from MEDIA_URL.
from django.conf.urls import url, include
from django.contrib import admin
from login_app import views as login_app_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/', login_app_views.login_user),
# creating registered namespaces for each app
url(r'^login/', include('login_app.urls', namespace = "login_app")),
url(r'^CMIRS/', include('dashboard_app.urls', namespace = "dashboard_app")),
url(r'^CMIRS/', include('submit_app.urls', namespace = "submit_app")),
url(r'^CMIRS/', include('filter_app.urls', namespace = "filter_app")),
url(r'^CMIRS/case/',include('report_app.urls', namespace = "report_app")),
url(r'^CMIRS/', include('search_app.urls', namespace = "search_app")),
url(r'^search/', include('haystack.urls')), ##used in navbar-search
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In an app report_app, I want the webpage to display a hyperlink that can be used to view an uploaded file. When I click on the hyperlink, I want it to request the URL to the uploaded file.
The upload looks like such in my models:
upload1 = models.FileField(upload_to = 'documents/%Y/%m/%d/')
I am having trouble figuring out what to use in the render(request) in my view and how to correctly code this in HTML. When I attempt to use "media", I get an error saying it cannot be matched.
Here is a snippet of the HTML I am trying:
<dt>Upload</dt><dd><tr><td>{{ case.upload1 }}</td></tr></dd>
I am also confused as how to set up my render(request) so that it knows to access media/, and then go to the correct documents/Y/M/D depending on the primary key.
You don't want to use the url tag here at all. Your media's URL is stored in your model, and has nothing to do with Django's path resolution logic. Just reference the url method of the field:
<a href="{{ case.upload1.url }}">
See the docs.
(Note also that serving files via your urls.py like this works in dev only; for prod you'll need to configure your webserver to do it.)

How to serve files for download in django?

I know that derivatives of this question have already been asked. But those questions are outdated and I would like to hear some new answers for new versions.
I have a model and it has a file field in it.
class MyModel(models.Model):
field = models.FileField()
I can upload files with this model by using the admin panel of django and I can set its location with the MEDIA_ROOT settings variable. But I can't download this file in the view. I have tried given its URL but I usually get the "404 not found" error.
def download(request):
file = # code to get the the model instance.
context = {'file': file}
return render(request, template, context)
Here is the code in template:
Download Link
This throws a 404 error. I know why it throw this error. Because no url deffinitions exist for that url.
So, how can I download this file?
Django 1.8.7, Python 3.4.3, ubuntu 14.04
In development, you can do this to get MEDIA_URL active
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django Development - Admin CSS file 404

I'm using this guide to attempt to get this working. Basically, I'm exploring django 1.6 (with python 2.7.6 on Mac OS X Yosemite beta), still working with the stock development server. I'm trying to include a CSS file to override some styles in the admin area. I have a static folder in my project root. My settings.py is completely stock (that means I have DEBUG set to true and that I'm using django.contrib.staticfiles). Inspecting the source and request/response reveals that I'm calling for the CSS file at my expected path, but that I'm getting a 404 when attempting to load it. I also get a 404 when attempting to hit the CSS file directly in the browser. I've searched google and SO and have not, as of yet, been able to find an answer.
The requested CSS file:
http://*mysite*/static/admin/css/mysite-admin.css
The file system path to the CSS file:
*myprojectdir*/static/admin/css/mysite-admin.css
Yes it won't work because Django does not serve by default static assets, in order to have static assets served (mind though this should be the case only for local dev) in your main urls.py file add this to the top:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
And then at the end of your urls.py:
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns
Make sure you have defined STATIC_URL, MEDIA_ROOT and MEDIA_URL in your settings.py, for development STATIC_ROOT IS NOT needed.

Django run admin inteface with custom template

In django how to run /admin interface as well as customized admin index page. My template dirs is followed below.
TEMPLATE_DIRS = (
PROJECT_PATH + '/templates/',
)
And...
ADMIN_MEDIA_PREFIX = '//admin/'
If i will comment this line my other functions would not work, if i put it uncommented then ma admin interface shows my specified file.
What should i do to run both simultaneously. Thanks in advance
Leave TEMPLATE_DIRS alone, that affects more than just the admin, and that's not your problem anyways.
The way to override any admin page is to include the associated template from the default Django admin templates in your own 'yourproject/templates/admin' directory, and make the necessary modifications.
See the documentation: https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#overriding-admin-templates

Categories

Resources