crispy-forms 在本地主机上尝试 运行 时出错
crispy-forms gives error when trying to run on local host
过去几天我一直在开发这个 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>'
这是我的 settings.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 上部署它不会有问题。
非常感谢您的帮助。如果您需要查看我的更多代码,请告诉我。
编辑:
我的项目结构:
-apotofgold
--apotofgold
---pycache
---迁移
----pycache
---静态
---模板
----apotofgold
---ENV(我的虚拟环境)
---媒体
---留言
----pycache
----迁移
---迁移
---静态
---模板
----apotofgold
---用户
----pycache
----迁移
----模板
编辑2:
views.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')
和模板login.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 %}
在你安装的应用程序中,crispy 表单应该这样声明
'crispy_forms'
过去几天我一直在开发这个 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>'
这是我的 settings.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 上部署它不会有问题。
非常感谢您的帮助。如果您需要查看我的更多代码,请告诉我。
编辑:
我的项目结构:
-apotofgold
--apotofgold
---pycache
---迁移
----pycache
---静态
---模板
----apotofgold
---ENV(我的虚拟环境)
---媒体
---留言
----pycache
----迁移
---迁移
---静态
---模板
----apotofgold
---用户
----pycache
----迁移
----模板
编辑2:
views.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')
和模板login.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 %}
在你安装的应用程序中,crispy 表单应该这样声明
'crispy_forms'