django ckeditor image upload - django

I'm using Django-ckeditor in my website.
I'm especially using
RichTextUploadingField()
in my model. and other option just works fine, except image upload.
1. Error Message
I'm getting an error message of
"Incorrect Server Response" and especially, chrome devtools indicates that
ckeditor.js:21 [CKEDITOR] Error code: filetools-response-error.
ckeditor.js:21 [CKEDITOR] For more information about this error go to https://docs.ckeditor.com/ckeditor4/docs/#!/guide/dev_errors-section-filetools-response-error
2. Guess
I have tried uploading images using ckeditor in my admin page,
authorized as superuser in django, it works.
However, logged in as the normal user account, I've tried the same thing, but it does not work.
So my guess is it has some kind of authorization problem. But I can't figure out where to start debugging in my django-ckeditor.
What things should I be checking? Thanks in advance.

This is happening because the default urls are decorated with #staff_member_required(https://github.com/django-ckeditor/django-ckeditor/blob/master/ckeditor_uploader/urls.py). To avoid this, instead of including the urls like so url(r'^ckeditor/', include('ckeditor_uploader.urls')) you could define them one by one in your urls.py with the login_required decorator:
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from ckeditor_uploader import views
urlpatterns = [
.....your other urls
url(r'^ckeditor/upload/', login_required(views.upload), name='ckeditor_upload'),
url(r'^ckeditor/browse/', never_cache(login_required(views.browse)), name='ckeditor_browse'),
]
Like this you are limiting the uploads to all users that are logged in.

Add following imports in the project urls.py:
from django.contrib.auth.decorators import login_required
from django.views.decorators.cache import never_cache
from ckeditor_uploader import views as ckeditor_views
Replace the following row in the urls.py:
path('ckeditor/', include('ckeditor_uploader.urls')),
with
path('ckeditor/upload/', login_required(ckeditor_views.upload), name='ckeditor_upload'),
path('ckeditor/browse/', never_cache(login_required(ckeditor_views.browse)), name='ckeditor_browse'),

it works if you are logged in as an admin(localhost:8000/admin), simple is that.

Related

Loading a .html file in django url with parameters

I have a file which I have to load directly through the web browser but it would still run inside of my django app.
What I have done so far is
url(r"^file_name.html", views.regular_person, name='regular_person')
But when I do this it shows the page could not be found. error 404
Please how can i get that to work.
Thanks
edit
the file would also have some query parameters
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('foo/', TemplateView.as_view(template_name='filename.html'))
]
django documentation
Is this something you are looking for.

Django Rest 'api-docs' is not a registered namespace

I'm setting the docs for my api and when I ask for (http://localhost:8000/api/v0/docs/) The app show me this error:
NoReverseMatch at /api/v0/docs/
'api-docs' is not a registered namespace
when try to do this
<script src="{% url **'api-docs:schema-js'** %}"></script>
this is my urls.py file:
from django.contrib import admin
from django.conf.urls import url, include
from django.contrib.auth.models import User
from . import views
from rest_framework import routers, serializers, viewsets
from rest_framework.documentation import include_docs_urls
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'goals', views.GoalViewSet)
urlpatterns = [
url(r'^goals/$', include(router.urls)),
url(r'^docs/', include_docs_urls(title='My API title')),
]
I added the namespace to include_docs_urls but it doesn't work. (I have installed coreapi, pygments and markdown).
Beforehand thanks for helping me.
You should move your docs url pattern into your main urls.py file. This is still a problem in DRF, there is also an issue opened on the official GitHub source: https://github.com/encode/django-rest-framework/issues/4984
The official developer of Django REST Framework, Tom Christie, says:
It's important enough that I'd like to see it highly prioritized, but there's a lot of other things going on at the moment too. Even after we release 3.6.3, there's still also all the work towards 3.7.0's realtime integration.
So your docs pattern should not have any prefixes (like api/v0/).
In conclusion, change your main urls.py:
url(r'^docs/', include_docs_urls(title='API Title'))

How to serve a static file in a Django app only to logged-in users?

I need to serve a particular static asset only to users logged into my Django site. I'm serving static assets through Apache. For various reasons I’m not interested in standing up a full CMS — I just need to make sure non-qualified site visitors cannot download this particular file.
This is really low traffic site so failing all else I can hardcode a urlpattern & serve it as a template.
But there's gotta be a smarter way to do this, right?
EDIT:
Here’s where I settled for now:
# views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
#login_required
def secretfile(request):
return render(request, 'secretfile.xls')
# urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'secretfile.xls', views.secretfile),
]

django-allauth caching login and signup pages

is there a way to set up Django Redis caching for login and signup views from django-allauth? I looked at docu and found nothing. I don't want whole site caching but only some views and these two are part of it.
Django Redis makes use of Django's caching framework. So the documentation bit you are looking for is here.
The short bit:
A more granular way to use the caching framework is by caching the output of individual views. django.views.decorators.cache defines a cache_page decorator that will automatically cache the view’s response for you.
For allauth, you'd need to match the login and signup URL before you include allauth.urls and then use the decorator in the url conf:
from django.views.decorators.cache import cache_page
from allauth.account.views import login
urlpatterns = [
url(r'^accounts/login$', cache_page(60 * 15)(login)),
# same for signup
url(r'^accounts/$', include('allauth.urls')
]

how to change a default django admin login view to generate token on login to admin site

my site.py:
from django.contrib.admin import AdminSite
class OptiAdminSite(AdminSite):
def get_urls(self):
from django.conf.urls import patterns, url, include
from core import views
from django.contrib.contenttypes import views as contenttype_views
urlpatterns = patterns('',
#url(r'^$', wrap(self.index), name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
return urlpatterns
opti_site = OptiAdminSite()
I'm developing an authentication API. When user logs in to my API it generates a code which get destroyed once user hit logout.
My problem is that whenever I'm running my API and django admin site in same browser, then if I login into admin-site It automatically login me in my API too with out any token. When I try to logout in that case from my API it generates an error - 'Token does not exist'. I want to generate token when admin user login to admin-site.
I've tried to do it with above trick as in official documentation but didn't find the right way to do it.
Please suggest me the correct way to do it. Is it necessary to make a separate app for it?
Thanks! in advance.
This solution is almost complete... Almost, because you're simply creating your own admin site in opti_site variable, but probably not using it anywhere.
To make it work, you can monkey-patch default admin site with your site, using:
from django.contrib import admin
admin.sites.site = opti_site
admin.site = admin.sites.site
Remember that you must do it before root urlpatterns definition (especially before defining urls to your admin site).
Another approach is to change default admin to your admin in include of url patterns:
url(r'^admin/', include(opti_site.urls)),