run django test on pycharm

If you get

django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

this error from line

from rest_framework.response import Response

Run | Edit Configurations | (e.g. Unittests in project) | Configuration | Environment | Environment variables: DJANGO_SETTINGS_MODULE=(project directory name).settings

Django logging

On local, Django is run by systemd and wsgi. So you can see the logs by journalctl on linux. But if it’s run by demon “gunicorn”, you need to specify log path.

So in server/settings/development.py

specify logging path like this.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt': "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
    }
}


on manage.py

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<server>.settings.development")

means you are using settings in development.py

Django add app

add app to settings/base.py INSTALLED_APPS

  • To import models in view.py

import models

dosen’t work

from .models import *

 

  • when you have encoding problem on csv file-> add encoding param

with open(out_file, r, encoding=utf-8) as csvFile: