django-cms, django flatpages, tiny mce not displaying - django

I've implemented both django-cms and flatpages, but can not get tiny_mce to display in either.
urls.py
(r'^tinymce/', include('tinymce.urls')),
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
settings.py
TINYMCE_JS_URL = 'http://127.0.0.1:8000/site_media/js/tiny_mce/tiny_mce.js'
TINYMCE_JS_ROOT = 'http://127.0.0.1:8000/site_media/js/tinymce/'
TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,spellchecker,paste,searchreplace",
'theme': "advanced",
'cleanup_on_startup': True,
'custom_undo_redo_levels': 10,
}
TINYMCE_SPELLCHECKER = False
TINYMCE_COMPRESSOR = False
TINYMCE_FILEBROWSER = True
CMS_USE_TINYMCE = True
admin.py
from django.contrib.flatpages.models import FlatPage
from django.contrib.flatpages.admin import FlatPageAdmin
#Flatpages
class FlatPageAdmin(FlatPageAdmin):
class Media:
js = ('http://127.0.0.1:8000/js/tiny_mce/tiny_mce.js',
'http://127.0.0.1:8000/js/tiny_mce/textareas.js',)
# We have to unregister it, and then reregister
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, FlatPageAdmin)
#django-cms
from myprograms.cms.models import Page
class PageOptions(admin.ModelAdmin):
class Media:
js = ('http://127.0.0.1:8000/site_media/js/tiny_mce/tiny_mce.js',
'http://127.0.0.1:8000/site_media/js/tiny_mce/textareas.js')
#admin.site.register(Page, PageOptions)
In the base.html file
<script type="text/javascript" src="{{ MEDIA_URL }}js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="{% url tinymce-js "NAME" %}"></script>
There are so many different options when accessing the various user groups, docs, etc. I'm not sure what is the correct syntax. The CMS doesn't do me much good without some kind of text editor.
Thx

first of all please check this line with slash like:
<script type="text/javascript" src="{{ MEDIA_URL }}/js/tiny_mce/tiny_mce.js"></script>
also please check site_id in error logs. had similar issue with site_id because I created new site with different id.
Best,
Mykola Lys.

If you need some more features then the simple flatpages just checkout django-blocks (http://code.google.com/p/django-blocks/). Has multi-language Menu, Flatpages and even has a simple Shopping Cart!!

Have you read the TinyMCE page on the Django wiki? Also - although it looks like it might not apply to you - browsers block calls from scripts across differing servers/domains...

Related

loading local files in django

I have a folder in c:\images, images here are updated by another app. Then I wanted a Django application to read those images on the admin site. The django applicationis sitting in c:\inetpub\wwwroot\myapp I have tried adding below lines in settings.py
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(CURRENT_PATH, '../../images').replace('\\','/')
MEDIA_URL = 'images/'
I also tried including the whole path in django admin as below in admin.py
def img_field(self, obj):
return format_html('<img src="{}" width="500" height="500" />'.format("c:\images\phot1.png"))
If i put online image link it works fine as below:
def img_field(self, obj):
img = "https://www.thebalancesmb.com/thmb/5G9LJXyFzbTVS-Fj_32sHcgJ8lU=/3000x0/filters:no_upscale():max_bytes(150000):strip_icc():format(webp)/start-online-business-with-no-money-4128823-final-5b87fecd46e0fb00251bb95a.png"
return format_html('<img src="{}" width="500" height="500" />'.format(img))
How can i get around this?
I got it I just needed to add the below lines in urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
#urls here
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
It is working fine.

Django Not showing Static Images but shows its alternative text(alt) by displaying["GET /static/image/down.jpg HTTP/1.1" 404 1771] in console window

I just simply want to show my static image in template.
I have looked every possible ways to solve this issue and try various approaches but could not get my static images in template. The alternative text of the respective image is shown but image itself not loaded and get this in terminal Window
"GET /static/image/down.jpg HTTP/1.1" 404 1771
here how my Django project is laid out is given below
project files hierarchy
in urls.py
from django.conf.urls import include,url
from django.contrib import admin
from django.urls import path
from myapp import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
.......]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
in settings.py
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL= "/media/"
in template question.html
i have tried different approaches to access the image[with {% load static %} tag on the top of html page]
like
<img src="{% static 'image/down.jpg' %}">
<img style="width: 245px; height: 247px;"
alt="Image description"
src="{% static 'image/down.jpg' %}"/>
<img src="/static/image/up.jpg" alt="Second-image" />
but could not get the problem solved
Make sure you have pillow installed correctly and if it still wont work, try reinstalling pillow.
Did you run: python manage.py collectstatic?
If not work, try:
{% load staticfiles i18n %}
In addition check django.contrib.staticfiles is included in your INSTALLED_APPS

Django ImageField Image Not Displaying

I'm trying to use Django 2.0. I'm using the development 'runserver' right now, so please keep in mind that I'm not in production yet. I am familiar with the differences between the two with regard to static files.
I have a Blog Post model. In the admin, I want to be able to add an image to my model using ImageField. I'd like it to be optional, and provide a default image if none is added.
I am able to load images in admin. I am unable to render my image in my template. The console shows that Django is trying to GET my image, but my configurations have not worked so far.
Here's my model and relevant field:
from PIL import Image
class Post(models.Model):
....
thumbnail = models.ImageField(default='/img/default.png', upload_to='img', blank=True, null=True)
....
My model works well aside from the ImageField. My View works well, and I don't think there is any issue there.
Here is my urls.py
....
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
....
path('posts/', views.PostListView.as_view(), name='post_list'),
path('post/<int:pk>/', views.PostDetailView.as_view(), name='post_detail'),
path('post/random/', views.random_post, name='random_post'),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I'm not at all sure about how to handle static files vs. media files in my urls.py, especially with changes to Django in version 2.0.
Here's my relevant settings.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
....
'django.contrib.messages.context_processors.media',
....
]
},
},
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
MEDIA_URL = '/media/'
I am equally unsure of what to put in my settings.py concerning media. My static CSS files are working just fine. I have a logo image in my static files that is rendering in my base template.
Here is my template tag for the ImageField:
<img class="card-image mr-3" src="{{ post.thumbnail.url }}" alt="Generic placeholder image">
Lastly, my /static folder and my /media folder are in the same directory. These are both in my app directory, /blog.
Can anyone see any blaring errors here, or can they make suggestions? My issue is the default image is not loading. The default image IS in the correct directory /media/img/. The console warning says "Not Found: /media/img/default.png".
Thanks in advance. Any suggestions will be helpful.
Django ImageField for Admin Panel
There is important thing you have to define your MEDIA_URL in urls.py.
Without this definition framework doesn't gives to you permission to access image
Setting.py
MEDIA_URL = '/uploads/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
urls.py
if settings.DEBUG: # new
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Models.py
class Product(models.Model):
title = models.CharField(max_length=150)
image=models.ImageField(blank=True,upload_to='images/')
def __str__(self):
return self.title
def image_tag(self):
return mark_safe('<img src="{}" height="50"/>'.format(self.image.url))
image_tag.short_description = 'Image'
Admin.py
class ProductAdmin(admin.ModelAdmin):
list_display = ['title','image_tag']
readonly_fields = ('image_tag',)
Printing the media root helped me solve this issue. Thanks Alasdair.
I don't think there was any issue with my model field above.
The urls.py code that I ended up using is directly from Django docs for Django 2.0 in handling static files https://docs.djangoproject.com/en/2.0/howto/static-files/.
The changes I made to Settings were done thanks to printing my MEDIA_ROOT in the console. I changed the syntax and the direction of a slash (blog\media). I also deleted the context processor shown above in my TEMPLATES options.
Here's what works for me:
models.py
thumbnail = models.ImageField(default='img/default.png', upload_to='img', blank=True, null=True)
urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'blog\media')
MEDIA_URL = '/media/'
print(MEDIA_ROOT)
Template tag
<img class="card-image mr-3" src="{{ post.thumbnail.url }}" alt="Generic placeholder image">

i18n Internationalization and Localization in React Django

I am working on a Project where I am using React js for Front-End and Django for backend. I need to implement i18n Internationalization and Localization
I saw Django documentation and came across django I18n javascript_catalog.
How to use the same using getText() in React JS?. Is there any other way to implement?.
Thanks in Advance
Update for: django > 2.0:
from django.views.i18n import JavaScriptCatalog
urlpatterns = [
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
]
Reference
Old:
Use below code in urls.py of project
from django.views.i18n import javascript_catalog
js_info_dict = {
'domain': 'djangojs',
'packages': ('name',)
}
urlpatterns += i18n_patterns(
url(r'^jsi18n/$', javascript_catalog, js_info_dict),
Add below line to your base html file
<script type="text/javascript" src="/jsi18n/"></script>

Django favicon.ico in development?

How can I serve favicon.ico in development? I could add a route in my urlconf, but I don't want that route to carry over to the production environment. Is there a way to do this in local_settings.py?
The easiest way would be to just put it in your static directory with your other static media, then specify its location in your html:
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico' %}"/>
My old answer was:
You can set up an entry in your urls.py and just check if debug is true. This would keep it from being served in production. I think you can just do similar to static media.
if settings.DEBUG:
urlpatterns += patterns('',
(r'^favicon.ico$', 'django.views.static.serve', {'document_root': '/path/to/favicon'}),
)
You also could just serve the favicon from your view.:
from django.http import HttpResponse
def my_image(request):
image_data = open("/home/moneyman/public_html/media/img/favicon.ico", "rb").read()
return HttpResponse(image_data, content_type="image/png")
This worked for me:
from django.conf.urls.static import static
...
if settings.DEBUG:
urlpatterns += static(r'/favicon.ico', document_root='static/favicon.ico')
From the docs:
from django.conf.urls.static import static
urlpatterns = patterns("",
# Your stuff goes here
) + static('/', document_root='static/')
There doesn't appear to be a way to serve a single static file, but at least this helper function is a wrapper which only works when DEBUG = True.
I use this:
from django import conf
from django.conf.urls import static
...
if conf.settings.DEBUG:
urlpatterns += static.static(
r"/favicon.ico", document_root=conf.settings.STATIC_ROOT / "favicon.ico"
)
Well, you can create your own loader.py file, which loads settings you want to override.
Loading this file should look like this:
try:
execfile(os.path.join(SETTINGS_DIR, 'loader.py'))
except:
pass
and be added at the end of settings.py.
This settings should not be commited into production server, only should appear on development machines. If you are using git, add loader.py into .gitignore.