Hasnain Sikander
Hasnain Sikander

Reputation: 511

ModuleNotFoundError: No module named ' django'

Hello there I am following a tutorial on YouTube I use postman to post request of a user to an api just following his tutorial. I found an error:

Internal Server Error: /
Traceback (most recent call last):
  File "E:\UsamaComputer\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "E:\UsamaComputer\env\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "E:\UsamaComputer\env\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "E:\UsamaComputer\env\lib\site-packages\django\template\response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "E:\UsamaComputer\env\lib\site-packages\django\template\response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "E:\UsamaComputer\env\lib\site-packages\django\template\loader.py", line 47, in select_template
    raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: index.html
[17/May/2021 12:38:41] "GET / HTTP/1.1" 500 80940
Internal Server Error: /favicon.ico
Traceback (most recent call last):
  File "E:\UsamaComputer\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "E:\UsamaComputer\env\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "E:\UsamaComputer\env\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "E:\UsamaComputer\env\lib\site-packages\django\template\response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "E:\UsamaComputer\env\lib\site-packages\django\template\response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "E:\UsamaComputer\env\lib\site-packages\django\template\loader.py", line 47, in select_template
    raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: index.html
[17/May/2021 12:38:41] "GET /favicon.ico HTTP/1.1" 500 80843
Bad Request: /auth/users/
[17/May/2021 12:38:45] "POST /auth/users/ HTTP/1.1" 400 58
Internal Server Error: /auth/users/
Traceback (most recent call last):
  File "E:\UsamaComputer\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "E:\UsamaComputer\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\UsamaComputer\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "E:\UsamaComputer\env\lib\site-packages\rest_framework\viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "E:\UsamaComputer\env\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "E:\UsamaComputer\env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "E:\UsamaComputer\env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception      
    raise exc
  File "E:\UsamaComputer\env\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "E:\UsamaComputer\env\lib\site-packages\rest_framework\mixins.py", line 19, in create
    self.perform_create(serializer)
  File "E:\UsamaComputer\env\lib\site-packages\djoser\views.py", line 144, in perform_create
    settings.EMAIL.activation(self.request, context).send(to)
  File "E:\UsamaComputer\env\lib\site-packages\templated_mail\mail.py", line 78, in send
    super(BaseEmailMessage, self).send(*args, **kwargs)
  File "E:\UsamaComputer\env\lib\site-packages\django\core\mail\message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "E:\UsamaComputer\env\lib\site-packages\django\core\mail\message.py", line 241, in get_connection
    self.connection = get_connection(fail_silently=fail_silently)
  File "E:\UsamaComputer\env\lib\site-packages\django\core\mail\__init__.py", line 34, in get_connection
    klass = import_string(backend or settings.EMAIL_BACKEND)
  File "E:\UsamaComputer\env\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "c:\users\hussnain\appdata\local\programs\python\python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named ' django'
[17/May/2021 12:38:52] "POST /auth/users/ HTTP/1.1" 500 155458

I search everywhere on google and do what I can to solve this error but all in vain. when I run pip freeze in the env it says I have installed django Django==3.2 when I tried pip install django it says requirement already satisfied I also tried installing it outside the environment but the same requirement already satisfied my WSGI.py file is at the correct place . Furthermore here is screen shot of my file in vscode . and here is my wsgi.py

"""
WSGI config for UsamaComputers project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'UsamaComputers.settings')

application = get_wsgi_application()

here is my settings.py file:

"""
Django settings for UsamaComputers project.

Generated by 'django-admin startproject' using Django 3.2.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-p=i3^g82t202a!nauup+(ysg--_mkpl35_#qgs_6pyj6gkx#)a'

# 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',


    'accounts',
    'djoser',

    "corsheaders",
    'rest_framework',
    'rest_framework.authtoken',
    'frontend',
     'rest_framework_simplejwt',

]

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',
    # thos is something i added and dont know what is that?
    'corsheaders.middleware.CorsMiddleware'
]

ROOT_URLCONF = 'UsamaComputers.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # os.path.join(BASE_DIR, '../frontend')
        'DIRS': [os.path.join(BASE_DIR, 'build')],
        '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 = 'UsamaComputers.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'shopingwebsite',
        'USER': 'postgres',
        'PASSWORD': '1234',
        'HOST': 'localhost'
    }
}
EMAIL_BACKEND = ' django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'fparxqsqikbnqhvm'
EMAIL_USE_TLS = True

# Password validation
# https://docs.djangoproject.com/en/3.2/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.2/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.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'build/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')


CORS_ORIGIN_ALLOW_ALL = True

REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
      'rest_framework.authentication.TokenAuthentication',
    #    'rest_framework.authentication.SessionAuthentication',
   ),
   'DEFAULT_PERMISSION_CLASSES': (
         'rest_framework.permissions.IsAuthenticated'
   ),
}
SIMPLE_JWT = {
    'AUTH_HEADER_TYPES': ('JWT',),
}
DJOSER = {
    'SET_USERNAME_RETYPE': True,
    'LOGIN_FIELD': 'email',
    'USERNAME_CHANGED_EMAIL_CONFIRMATION': True,
    'PASSWORD_CHANGED_EMAIL_CONFIRMATION': True,
    'SEND_CONFIRMATION_EMAIL': True,
    'SET_PASSWORD_RETYPE': True,
    'SET_USERNAME_RETYPE': True,
    'PASSWORD_RESET_CONFIRM_URL': 'password/reset/confirm/{uid}/{token}',
    'USERNAME_RESET_CONFIRM_URL': 'email/reset/confirm/{uid}/{token}',
    'ACTIVATION_URL': 'activate/{uid}/{token}',
    'SEND_ACTIVATION_EMAIL': True,
    'SETIALIZER': {
        'user_create':'accounts.serializers.UserCreateSerializer',
        'user':'accounts.serializers.UserCreateSerializer',
      
        'user_delete':'djoser.serializers.UserDeleteSerializer'

    }
}


AUTH_USER_MODEL = 'accounts.UserAccount'

here is the error in postman: screenshot without postman it give me same the error. any help would be highly appreciated Thanks a lot.

Upvotes: 0

Views: 3109

Answers (1)

Hasnain Sikander
Hasnain Sikander

Reputation: 511

Error was in the extra space as said by @AbdulAziz

EMAIL_BACKEND = ' django.core.mail.backends.smtp.EmailBackend' notice that space just before the django.core there (after the ')? That is the cause of your error. Remove the space hence it should be EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' –

Upvotes: 1

Related Questions