Django中的ModuleNotFound错误。无法解决错误

时间:2019-12-04 05:57:22

标签: python django python-3.x

我不明白该错误。我之前做过一个项目,但是virtualenv遇到了一些问题,所以我用virtualenv创建了另一个目录,然后将所有代码复制粘贴到所有文件中,包括manage.py settings.py和urls.py。我在virtualenv中使用命令行启动了应用程序,然后复制了先前应用程序中粘贴的所有代码。

manage.py文件

#!/usr/bin/env python
Django's command-line utility for administrative tasks."""
import os
import sys


def main():

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'quicktt.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

输出(错误)

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 8, in main
    os.environ.setdefault('DJANGO_SETTINGS_MODULE')
TypeError: setdefault() missing 1 required positional argument: 'value'
(quicktt) udaykhalsa@warmachine:~/Projects/quick_timetable_main$ python manage.py runserver
Traceback (most recent call last):
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 67, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/conf/__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/conf/__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'quicktt.settings'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/core/management/base.py", line 336, in run_from_argv
    connections.close_all()
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/db/utils.py", line 219, in close_all
    for alias in self:
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/db/utils.py", line 213, in __iter__
    return iter(self.databases)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/db/utils.py", line 147, in databases
    self._databases = settings.DATABASES
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/conf/__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/site-packages/django/conf/__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/udaykhalsa/Projects/quick_timetable_main/quicktt/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'quicktt.settings'

1 个答案:

答案 0 :(得分:1)

更改此行

$('.tabledit-confirm-button').click(function () {
    if(confirm("Are you sure, you want to delete this category?")) {
        var data = $(this).closest('tr').find('input[type=hidden]').val();
        var $this = $(this);

        var url = '{{ route("category.destroy", ":data") }}';

        url = url.replace(':data', data);

        $.ajax({
            method: "POST",
            url: url,
            dataType: 'json',
            data: { _token:"{{ csrf_token() }}", _method:"DELETE"},
            success(response){

                success(response.data);

                $this.parent("tr").remove()

            },
            error: function(response){
                let data = response.responseJSON.errors;

                $.each( data, function( key, value ) {
                    $( "<span class='validation-errors text-danger'>"+value+"</span>" ).insertAfter( "#"+key );
                });
            }
        });
    }

});

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

Django正在尝试在os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'quick_timetable.settings') 内查找settings.py文件,该文件不存在,因为这是一个新的Django项目,并且设置文件位于quicktt内部。