No 'Access-Control-Allow-Origin' header is present on the requested resource. - python

I am using ajax to call the following url:
"https://www.bittrex.com/api/v1.1/public/getticker?market=usdt-btc"
I am getting the following error:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have installed django-cors-headers and my settings.py looks like this:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'crispy_forms',
# The following apps are required:
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'http://127.0.0.1:8000',
)
Why am I still getting the error?
** I have read all the other answers related to this but they have not helped me. I have used the chrome plugin too and it does work with it but I want a solution without it **
Django Version = 1.8

The configuration looks fine and according to the documentation. I just compared it to my configuration and it's almost the same. Since I use my API for only one website, I have it configured in my CORS_ORIGIN_WHITELIST, you can try this also to see if it responds.
Also, try removing all python cache files and restart the application server, maybe the setting where not loaded.

It is not your app issue i.e. django issue as CORS is a http header which let's a user agent gain permission to a resource which is on a different domain as that of yours. It is a browser issue, hence adding CORS application in django will have no effect.
More information here
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Try adding crossDomain: true in the ajax request

Related

Django debug toolbar loads but doesn't show on the local server even after proper installation

I have installed django debug toolbar correctly on a pipenv environment and done the main steps, such as adding internal_ips of 127.0.0.1 and adding the debug toolbar to installed_apps and middleware but still whenever I run the server and open a template of an app the debug toolbar doesn't show.
However if I open the page source in the browser, it shows all the list elements of the toolbar however the toolbar doesn't display, does anyone know what is going wrong and how I could fix it.
This are the changes made to the settings.py file:
import mimetypes
mimetypes.add_type("application/javascript", ".js", True)
mimetypes.add_type("text/css", ".css", True)
mimetypes.add_type("text/html", ".html", True)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'playground',
'debug_toolbar',
]
MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
INTERNAL_IPS = [
'127.0.0.1',
]
And the following changes to the urls.py folder:
import debug_toolbar
urlpatterns = [
...,
path('__debug__/', include(debug_toolbar.urls))
]

How to add 'django.contrib.auth' app in django

I deployed Django project using cloud code from pycharm.
but in this sample template, there were not the code in installed_app basic apps like these
'django.contrib.admin', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.staticfiles',
there was only 'django.contrib.staticfiles' apps.
Also the default middleweres were not completely installed
INSTALLED_APPS = [
'django.contrib.staticfiles',
'helloapp',
]
MIDDLEWARE = [
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
So, is there any way to use default apps to make login page.?

cross origin access issues - django 2.1.7

I have gone through literally all SO links, reinstalled django and django-cors-headers and followed this to the T and yet we get
pre flight error cross origin not allowed
Django version 2.1.7
relevant sections of settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'uploads.core',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsPostCsrfMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CORS_ORIGIN_ALLOW_ALL = True
CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django’s CommonMiddleware or Whitenoise’s WhiteNoiseMiddleware. If it is not before, it will not be able to add the CORS headers to these responses.
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware', # <-- should be at the top
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsPostCsrfMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
This is an issue is because the given Django package is for Djangov2.2>
Here you are using 2.1 so it doesn't support it.
You need to manually pass headers from HttpResponse

Django debug toolbar works only in admin interface

I've did all the steps which are described in the docs and got the toolbar in admin site. However, I can't get it work in the project frontend side.
Here is my settings:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
...
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
INTERNAL_IPS = ('127.0.0.1', 'localhost',)
I've googled a lot already and non of the proposed solution are not working for me: workaround that shows toolbar even if debug=False, playing with different parameters like INTERCEPT_REDIRECTS=False/True, specifying JQUERY_URL since there is a local jquery which is used by frontend.
I've asked for help from frontend dev, but he can't understand why there is no any configs for client side.
Frontend is using nodes.js and listens on 3000 port than forwards to localhost:8081.
Django version 1.10, django-debug-toolbar 1.9.1.

Django settings.py not being detected

I'm pretty new to Django and I'm having some difficulty getting my settings.py to load properly. I'm getting the following error:
ImproperlyConfigured at /admin
Put 'django.contrib.admin' in your INSTALLED_APPS setting in order to
use the admin application.
However, my settings.py INSTALLED_APPS looks as follows:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'squaredcrm' )
Looking through the error log, I've noticed its not picking up any of my changes to installed apps:
Django Version: 1.4.3 Python Version: 2.7.3 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sites',
'django.contrib.messages', 'django.contrib.staticfiles') Installed
Middleware: ('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
I cannot figure this out for the life of me. Any other changes seem to be working, but this field will not update. Any ideas?
I know it's a silly question, but did you restart your server after making the changes?
By default, production (by which I mean Apache-based, and perhaps other) instances of Django do not auto-reload on changes. The Django development server will auto-reload, as long as you don't specifically tell it not to.
You have to restart (or stop and then start) an Apache-based Django for it to see the file changes.
Important tip: do not run a production site off of the development server. It is slow, slow, slow, and probably insecure in ways I don't know about.
If other changes are picked up, this is probably because INSTALLED_APPS is being redefined somewhere in your settings.py file.
This could be:
At a subsequent line.
In an import (likely a from x import *).

Categories