crispyforms在尝试在本地hos上运行时出错

2024-04-20 09:33:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为这个django应用程序工作了几天,但还没有找到一个解决方案,使它工作。我试图在本地主机和heroku上部署它,但是由于这个错误,两者都不起作用。你知道吗

下面是我尝试运行时收到的错误消息

    python manage.py runserver

。。。你知道吗

    Watching for file changes with StatReloader
    Exception in thread django-main-thread:
    Traceback (most recent call last):
      File "c:\users\gorma\appdata\local\programs\python\python36-                        32\Lib\threading.py", line 916, in _bootstrap_inner
        self.run()
      File "c:\users\gorma\appdata\local\programs\python\python36-        32\Lib\threading.py", line 864, in run
        self._target(*self._args, **self._kwargs)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 54, in wrapper
        fn(*args, **kwargs)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\commands\runserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 77, in raise_last_exception
        raise _exception[1]
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\__init__.py", line 337, in execute
        autoreload.check_errors(django.setup)()
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 54, in wrapper
        fn(*args, **kwargs)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\apps\registry.py", line 91, in populate
        app_config = AppConfig.create(entry)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\apps\config.py", line 90, in create
        module = import_module(entry)
      File "C:\Projects\new\apotofgold\ENV\lib\importlib\__init__.py",         line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 994, in _gcd_import
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 953, in         _find_and_load_unlocked
    ModuleNotFoundError: No module named 'crispy-forms'

    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 "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\__init__.py", line 381, in         execute_from_command_line
        utility.execute()
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\__init__.py", line 375, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\base.py", line 323, in run_from_argv
        self.execute(*args, **cmd_options)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\commands\runserver.py", line 60, in execute
        super().execute(*args, **options)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\base.py", line 364, in execute
        output = self.handle(*args, **options)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\commands\runserver.py", line 95, in handle
        self.run(**options)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\core\management\commands\runserver.py", line 102, in run
        autoreload.run_with_reloader(self.inner_run, **options)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 587, in run_with_reloader
        start_django(reloader, main_func, *args, **kwargs)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 572, in start_django
        reloader.run(django_main_thread)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 290, in run
        self.run_loop()
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 296, in run_loop
        next(ticker)
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 336, in tick
        for filepath, mtime in self.snapshot_files():
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 352, in snapshot_files
        for file in self.watched_files():
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 251, in watched_files
        yield from iter_all_python_module_files()
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 103, in                 iter_all_python_module_files
        return iter_modules_and_files(modules, frozenset(_error_files))
      File "C:\Projects\new\apotofgold\ENV\lib\site-        packages\django\utils\autoreload.py", line 138, in iter_modules_and_files
        if not path.exists():
      File "c:\users\gorma\appdata\local\programs\python\python36-        32\Lib\pathlib.py", line 1314, in exists
        self.stat()
      File "c:\users\gorma\appdata\local\programs\python\python36-        32\Lib\pathlib.py", line 1136, in stat
        return self._accessor.stat(self)
      File "c:\users\gorma\appdata\local\programs\python\python36-        32\Lib\pathlib.py", line 387, in wrapped
        return strfunc(str(pathobj), *args)
    OSError: [WinError 123] The filename, directory name, or volume label         syntax is incorrect: '<frozen importlib._bootstrap>'

这是我的设置.py你知道吗

    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__)))

    TEMPLATE_DIR = os.path.join(BASE_DIR,"templates")

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

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = 'n(xni*3g-2+tzjr48176q^jmbx5$l#nb196z@hu-%j9x+h5gx_'

    # 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',
        'message',
        'users',
        'crispy-forms',
        'bootstrap4'
        ,
    ]
    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 = 'apotofgold.urls'

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [TEMPLATE_DIR],
            'APP_DIRS': True,
            'OPTIONS': {
v                '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 = 'apotofgold.wsgi.application'


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

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/2.2/howto/static-files/
    STATIC_ROOT = ''

    STATIC_URL = '/static/'

    STATICFILES_DIRS =                                         [os.path.join(BASE_DIR,"static"),'C:\Projects\new\apotofgold\apotofgold\static        ']
    STATICFILES_FINDERS =         ['django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    ]
    CRISPY_TEMPLATE_PACK = 'bootstrap4'

    LOGIN_REDIRECT_URL = 'message-home'

    LOGIN_URL = 'login'

    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

现在在本地部署它会很好,但是如果我能消除这个错误,我相信在heroku上部署它不会有问题。你知道吗

非常感谢你的帮助。如果你需要查看我的任何代码,请让我知道更多。你知道吗

编辑:

我的项目结构:

-黄金

——黄金

---pycache

---迁移

---pycache

---静态

---模板

----黄金

---ENV(我的虚拟环境)

---媒体

---消息

---pycache

----迁移

---迁移

---静态

---模板

----黄金

---用户

---pycache

----迁移

----模板

编辑2:

视图.py对于用户:

    from django.shortcuts import render, redirect
    from django.contrib.auth.forms import UserCreationForm
    from django.contrib import messages
    from .forms import UserRegistrationForm
    from django.contrib.auth.decorators import login_required

    def register(request):
        #form = UserCreationForm(request.POST)
        if request.method == 'POST':
            form = UserRegistrationForm(request.POST)
            if form.is_valid():
                form.save()
                username = form.cleaned_data.get('username')
                messages.success(request, f'Account created for         {username}!')
            return redirect('message-home')
        else:
    form = UserRegistrationForm()
        return render(request, 'users/register.html',{'form':form})

    @login_required
    def profile(request):
        if request.method == 'POST':
            if request.user.userprofile is None:
                user_profile = UserProfile(user=request.user)
                user_profile.save
        return render(request, 'users/profile.html')

以及模板登录.html你知道吗

    {% extends "apotofgold/base.html" %}
    {% block content %}
        {% load crispy_forms_tags %}
            <div class = "content-section">
                <form method="POST">
                    {% csrf_token %}
                    <fieldset  class="form-group">
                                <legend class="border-bottom mb-4">Log         In</legend>
                        {{ form|crispy }}
                    </fieldset>
                    <div class="form-group">
                        <button class="btn btn-outline-info"         type="submit">Log In</button>
                </form>
                <div class="border-top pt-3">
                    <small class="text-muted">Need an account?<a href="{%         url 'register' %}">Sign Up Now!</a>
                    </small>
                </div>
            </div>
    {% endblock content %}

Tags: djangoruninpyselfenvnewlib