Issue in django.core.exceptions.ImproperlyConfigured - django-rest-framework

I added social_django.middleware.SocialAuthExceptionMiddleware
MIDDLEWARE = [
'social_django.middleware.SocialAuthExceptionMiddleware',
'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',
]
Error:
django.core.exceptions.ImproperlyConfigured: WSGI application
'Project2.wsgi.application' could not be loaded; Error importing
module.

You defined somewhere in your settings file the following line:
WSGI_APPLICATION = 'Project2.wsgi.application'
That means you must have under your Project2 directory a wsgi.py file that declare the wsgi application. It should look like this:
import os
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
More details in the official doc here.

Related

djangorestframework-simplejwt not working

I'm following a Udemy Course with user administration and I need to install this
pip install djangorestframework-simplejwt
After its installed, I need to add this to settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
}
But its gives me this error
ImportError: Could not import 'rest_framework_simplejwt.authentication.JWTAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ModuleNotFoundError: No module named 'pkg_resources'.
So far I haven't been been able to find anything on google
INSTALLED_APPS = [
...
'rest_framework_simplejwt',
...
]
It should be added in INSTALLED_APPS
Source: https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html

Importing allauth views causes Runtime error: django.contrib.sites.models.Site doesn't declare an explicit app_label

I have tried all the SO questions I could find, but I still cannot resolve my issue. I am switching my Django project to DRF, and first thing I did was to bring in django-allauth.
Here's the necessary bit of my settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'taggit',
'taggit_serializer',
# my apps
'users', # CustomUser model
...
# DRF stuff
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
# debug in dev
'django_extensions',
'shell_plus',
'livereload',
'debug_toolbar',
]
SITE_ID = 1
Anytime I apply mange.py makemigrations, I get the following error:
RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an a
pplication in INSTALLED_APPS.
After inspecting, I found that anytime I would use any of the allauth.account.views imports in my urls.py, I get this error. It doesn't even matter if I use the import or not, just importing alone is enough to trigger the error. How can I proceed?
I am using Django 3.1.7, and django allauth 0.44.0.

How can add "templates " and "statics" folder in Django 3.1

setting.py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
#TEMPLATES_DIR = path.join(BASE_DIR, 'templates') #it dosen't work
#STATICS_DIR = path.join(BASE_DIR, 'statics') #it also dosen't work
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0#nf#$8nm5jb0)meuq&j6kztt534#nhk)k&$zh=#emcxi7_7fo'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'musicianapp',
]
MIDDLEWARE = [
'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',
]
ROOT_URLCONF = 'musicianList.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'musicianList.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATICS_DIR,
]
Please consern on the top and 3rd top line where is the main difference from Djago2.7 version
In Djnago2.7 where imported os as below,
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
and I used the below code for connecting templates and statics forlder
TEMPLATES_DIR =os.path.join(BASE_DIR,"templates")
STATICS_DIR =os.path.join(BASE_DIR,"statics")
But there is differece in form Djnago 3.1 from pathlib imported Path given below,
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
and the os techinques dosen't works as below
TEMPLATES_DIR =os.path.join(BASE_DIR,"templates")
STATICS_DIR =os.path.join(BASE_DIR,"statics")
How can I make connection with templates and statics folder, need help.
Note: I create templates and statics folders in main projects where already presented manage.py and db.sqlite3 file. below the screenshot of my projects direction,
If you look at the top of the settings.py file, you'll see BASE_DIR is using pathlib:
BASE_DIR = Path(__file__).resolve().parent.parent
Because of this, you no longer need os to join the paths, you can simply do:
STATICS_DIR = BASE_DIR / "statics"
This is new in 3.1 - you can read about it in the release notes here: https://docs.djangoproject.com/en/3.1/releases/3.1/
I would also recommend https://docs.djangoproject.com/en/4.0/howto/overriding-templates/, but if nothing happens then it should be the Refactoring problem especially when using PyCharm. So, you need to revert your changes. It worked for me.

Django 2.0.6 manage.py getting runserver error

I'm new on python and also try to learn a Django version 2.0.6 , so when i try to write the code with the documentation of Django here:
If faced a problem, specifically when I want to run the code, could help me to solve that issue please.
1- code (urls.py)folder polls:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name= 'index'),
]
2- code (urls.py) folder mysite:
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/',include('polls.Urls'))
]
3- code (views.py) folder mysite:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hi world, you are walcome")

Dajaxice 0.5.1 - Can't seem to make the tutorial work

I've checked over my stuff repeatedly and I cannot find the issue.
Here's the layout of my project:
project
allthings
init.py
admin.py
settings.py
models.py
views.py
tests.py
static
css
dajaxice
dajaxice.core.js
images
js
templates
testing.html
project
ajax.py
init.py
settings.py
urls.py
views.py
wsgi.py
manage.py
settings.py
DEBUG = True
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL =/static/
STATICFILES_DIRS = (
'C:/Python27/djcode/project/static',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'dajaxice.finders.DajaxiceFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'project.urls'
WSGI_APPLICATION = 'project.wsgi.application'
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages'
)
TEMPLATE_DIRS = (
"C:/Python27/djcode/project/templates"
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'allthings',
'dajaxice',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
)
urls.py
from django.conf.urls import patterns, include, url
from project.views import hello, testing
from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
('^hello/$', hello),
('^testing/$', testing),
url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += staticfiles_urlpatterns()
views.py
from django.shortcuts import render_to_response, HttpResponse
from django.template import RequestContext
from dajaxice.core import dajaxice_autodiscover
dajaxice_autodiscover()
def hello(request):
return HttpResponse("Hello world!")
def testing(request):
return render_to_response('testing.html', context_instance = RequestContext(request))
ajax.py
from django.utils import simplejson
from dajaxice.decorators import dajaxice_register
#dajaxice_register
def sayhello(request):
return simplejson.dumps({'message':'Hello World'})
testing.html
<!DOCTYPE html/>
{% load dajaxice_templatetags %}
<html>
<head><title></title>
{% dajaxice_js_import %}
<script>
function my_callback(data){
alert(data.message);
}
</script>
</head>
<body>
This is to test stuff
<input type="button" onclick="Dajaxice.project.sayhello(my_callback)" value="Get Message from Server"></input>
</body>
</html>
All it does it make a button that puts out an alert. It should be incredibly simple and yet I get nothing. What am I doing wrong?
Because dajaxice.core.js is a non-static template file, DajaxFinder will find it and 'render' to a temp folder as a static file.
So your STATICFILES_DIRS settings should not contain the file path to dajaxice.core.js otherwise the file will be found by FileSystemFinder rather than DajaxFinder.
You can run findstatic to check it is found correctly or not:
manage.py findstatic dajaxice\dajaxice.core.js
or maybe:
manage.py findstatic dajaxice/dajaxice.core.js
The output result should be in some temp folder (depending on your OS) and should not be in any folder of your app.

Resources