我是Python和Django的新手,请原谅我,如果这个问题看起来很愚蠢。我跟随django tutorial并且在您使用自己的套餐时遇到了这个步骤
应用mysite
有效且没有错误。
我已根据教程设置了所有必需的文件。
我已经python3 setup.py sdist
(在django-polls文件夹中)
django-polls-0.1.tar.gz
django-polls/dist/
个文件
$ cd ..
$ pip3 install --user django-polls/dist/django-polls-0.1.tar.gz
Processing ./django-polls/dist/django-polls-0.1.tar.gz
Requirement already satisfied (use --upgrade to upgrade): django-polls==0.1 from file:///home/viet/webdev/django-polls/dist/django-polls-0.1.tar.gz in /home/viet/.local/lib/python3.6/site-packages
Building wheels for collected packages: django-polls
Running setup.py bdist_wheel for django-polls ... done
Stored in directory: /home/viet/.cache/pip/wheels/ff/ec/8b/85c031efa2ba719d6bd909d20f61ca6b106c79345a1e875f08
Successfully built django-polls
然后教程说:
幸运的是,您的Django项目现在应该再次正常工作。跑 服务器再次确认这一点。
如何? manage.py
或/django-polls
/home/viet/.cache/pip/wheels/ff/ec/8b/85c031efa2ba719d6bd909d20f61ca6b106c79345a1e875f08
个文件
所以我尝试了这个:
$ pip3 uninstall django-polls
Uninstalling django-polls-0.1:
...
Proceed (y/n)? y
Successfully uninstalled django-polls-0.1
$ pip3 install --user django-polls/dist/django-polls-0.1.tar.gz
Processing ./django-polls/dist/django-polls-0.1.tar.gz
Building wheels for collected packages: django-polls
Running setup.py bdist_wheel for django-polls ... done
Stored in directory: /home/viet/.cache/pip/wheels/ff/ec/8b/85c031efa2ba719d6bd909d20f61ca6b106c79345a1e875f08
Successfully built django-polls
Installing collected packages: django-polls
Successfully installed django-polls-0.1
我此时已经迷失了。
更新
感谢您的建议,我现在理解我需要将文件夹/polls
从我的文件夹/mysite
移到文件夹/django-polls
所以树看起来像这样:
~/webdev$ ls -a
django-polls mysite
$ cd django-polls/
s$ ls -a
. dist docs MANIFEST.in README.rst
.. django_polls.egg-info LICENSE polls setup.py
我参加了/webdev
:
$ pip3 install --user django-polls/dist/django-polls-0.1.tar.gz
Processing ./django-polls/dist/django-polls-0.1.tar.gz
...
Successfully built django-polls
$ cd mysite/
$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
No migrations to apply.
$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
April 09, 2018 - 09:29:25
Django version 2.0.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
我去localhost:8000 /民意调查:
Internal Server Error: /polls/
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 158, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 156, in _get_response
response = response.render()
File "/usr/local/lib/python3.6/dist-packages/django/template/response.py", line 106, in render
self.content = self.rendered_content
File "/usr/local/lib/python3.6/dist-packages/django/template/response.py", line 81, in rendered_content
template = self.resolve_template(self.template_name)
File "/usr/local/lib/python3.6/dist-packages/django/template/response.py", line 63, in resolve_template
return select_template(template, using=self.using)
File "/usr/local/lib/python3.6/dist-packages/django/template/loader.py", line 47, in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: polls/index.html, polls/question_list.html
[09/Apr/2018 09:29:28] "GET /polls/ HTTP/1.1" 500 83106
我刚将整个/polls
复制到/django-polls
并且构建成功了,为什么我在/polls
/mysite
内Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
) from exc
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?
工作正常时会出现TemplateDoesNotExist错误?< / p>
更新2018-04-30
新错误:
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
之前运行正常,我尝试在虚拟环境中启动服务器,但该应用程序无法识别已安装的Django。
答案 0 :(得分:2)
本教程packaging your app section中的第2步指示将民意调查应用程序从项目目录移至django-polls。
如果此时你运行开发服务器,django将抛出一个异常,因为polls在已安装的应用程序中列出,但在python可见的任何路径中都不存在。
安装软件包时,pip会在python安装的site-packages目录中安装django-polls软件包。
此时,运行migrate,应该使用site-packages中的民意调查应用程序。
答案 1 :(得分:1)
1)对于你问题的第一部分:我很想念,抱歉。 @Paulo和@Vishal已经很好地解释了。
2)对于第二部分:您已经成功安装了django-polls
app,所以
您的服务器运行并找到polls
应用,但无法找到'polls / index.html'模板。这就是为什么你看到下面的错误:
TemplateDoesNotExist: polls/index.html, polls/question_list.html
您应该检查模板文件是否在正确的位置。
如果您在虚拟环境中安装了path_to_virtualenv\Lib\site-packages\polls\templates\polls
应用,则模板位于django-polls
目录中。否则(系统范围的安装),您可以查看python_path\Lib\site-packages\polls\templates\polls
到目前为止,你可能已经解决了这个问题,很抱歉由于某些原因最近回答。我希望它有所帮助。
答案 2 :(得分:1)
这是我的案子出现错误:
django.template.exceptions.TemplateDoesNotExist:民意调查/index.html,民意调查/question_list.html
我遵循此指南:https://docs.djangoproject.com/en/2.2/intro/reusable-apps/
而且,我正在使用virtualenv
。
这是我的文件夹结构:
(venv) ☁ mrdulin tree -L 1 | grep 'django'
├── django-codelab
├── django-polls
由MANIFEST.in
文件的文件名引起的错误。我从指南中复制了文件名。但是不知何故,文件名的开头有一个空白空间。这意味着文件名是
当我运行python setup.py sdist
命令时,输出如下:
(venv) ☁ django-codelab [master] ⚡ ..
(venv) ☁ mrdulin cd django-polls
(venv) ☁ django-polls python setup.py sdist
running sdist
running egg_info
creating django_polls.egg-info
writing django_polls.egg-info/PKG-INFO
writing dependency_links to django_polls.egg-info/dependency_links.txt
writing top-level names to django_polls.egg-info/top_level.txt
writing manifest file 'django_polls.egg-info/SOURCES.txt'
reading manifest file 'django_polls.egg-info/SOURCES.txt'
writing manifest file 'django_polls.egg-info/SOURCES.txt'
running check
creating django-polls-0.1
creating django-polls-0.1/django_polls.egg-info
creating django-polls-0.1/polls
creating django-polls-0.1/polls/migrations
copying files to django-polls-0.1...
copying README.rst -> django-polls-0.1
copying setup.cfg -> django-polls-0.1
copying setup.py -> django-polls-0.1
copying django_polls.egg-info/PKG-INFO -> django-polls-0.1/django_polls.egg-info
copying django_polls.egg-info/SOURCES.txt -> django-polls-0.1/django_polls.egg-info
copying django_polls.egg-info/dependency_links.txt -> django-polls-0.1/django_polls.egg-info
copying django_polls.egg-info/top_level.txt -> django-polls-0.1/django_polls.egg-info
copying polls/__init__.py -> django-polls-0.1/polls
copying polls/admin.py -> django-polls-0.1/polls
copying polls/apps.py -> django-polls-0.1/polls
copying polls/models.py -> django-polls-0.1/polls
copying polls/tests.py -> django-polls-0.1/polls
copying polls/urls.py -> django-polls-0.1/polls
copying polls/views.py -> django-polls-0.1/polls
copying polls/views_old.py -> django-polls-0.1/polls
copying polls/migrations/0001_initial.py -> django-polls-0.1/polls/migrations
copying polls/migrations/__init__.py -> django-polls-0.1/polls/migrations
Writing django-polls-0.1/setup.cfg
creating dist
Creating tar archive
removing 'django-polls-0.1' (and everything under it)
如您所见,templates
,static
,docs
目录中的文件未复制到dist目录。这就是原因。
从MANIFEST.in
文件的文件名中删除空格后,再次运行python setup.py sdist
命令,输出为:
(venv) ☁ django-polls python setup.py sdist
running sdist
running egg_info
creating django_polls.egg-info
writing django_polls.egg-info/PKG-INFO
writing dependency_links to django_polls.egg-info/dependency_links.txt
writing top-level names to django_polls.egg-info/top_level.txt
writing manifest file 'django_polls.egg-info/SOURCES.txt'
reading manifest file 'django_polls.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'django_polls.egg-info/SOURCES.txt'
running check
creating django-polls-0.1
creating django-polls-0.1/django_polls.egg-info
creating django-polls-0.1/docs
creating django-polls-0.1/polls
creating django-polls-0.1/polls/migrations
creating django-polls-0.1/polls/static
creating django-polls-0.1/polls/static/polls
creating django-polls-0.1/polls/static/polls/images
creating django-polls-0.1/polls/templates
creating django-polls-0.1/polls/templates/polls
copying files to django-polls-0.1...
copying LICENSE -> django-polls-0.1
copying MANIFEST.in -> django-polls-0.1
copying README.rst -> django-polls-0.1
copying setup.cfg -> django-polls-0.1
copying setup.py -> django-polls-0.1
copying django_polls.egg-info/PKG-INFO -> django-polls-0.1/django_polls.egg-info
copying django_polls.egg-info/SOURCES.txt -> django-polls-0.1/django_polls.egg-info
copying django_polls.egg-info/dependency_links.txt -> django-polls-0.1/django_polls.egg-info
copying django_polls.egg-info/top_level.txt -> django-polls-0.1/django_polls.egg-info
copying docs/.gitkeep -> django-polls-0.1/docs
copying polls/__init__.py -> django-polls-0.1/polls
copying polls/admin.py -> django-polls-0.1/polls
copying polls/apps.py -> django-polls-0.1/polls
copying polls/models.py -> django-polls-0.1/polls
copying polls/tests.py -> django-polls-0.1/polls
copying polls/urls.py -> django-polls-0.1/polls
copying polls/views.py -> django-polls-0.1/polls
copying polls/views_old.py -> django-polls-0.1/polls
copying polls/migrations/0001_initial.py -> django-polls-0.1/polls/migrations
copying polls/migrations/__init__.py -> django-polls-0.1/polls/migrations
copying polls/static/polls/style.css -> django-polls-0.1/polls/static/polls
copying polls/static/polls/images/background.jpg -> django-polls-0.1/polls/static/polls/images
copying polls/templates/polls/detail.html -> django-polls-0.1/polls/templates/polls
copying polls/templates/polls/index.html -> django-polls-0.1/polls/templates/polls
copying polls/templates/polls/results.html -> django-polls-0.1/polls/templates/polls
Writing django-polls-0.1/setup.cfg
creating dist
Creating tar archive
removing 'django-polls-0.1' (and everything under it)
(venv) ☁ django-polls
现在,template
,static
,docs
目录中的文件已复制到dist目录。
然后,将目录更改为django-codelab
,运行以下命令:
pip3 install ../django-polls/dist/django-polls-0.1.tar.gz
将polls
应用程序添加到INSTALLED_APPS
列表中:
INSTALLED_APPS = [
'polls',
# 'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
运行服务器:
python manage.py runserver
它完美地工作! 因此,本教程是正确的。请检查您的步骤,并仔细编码
答案 3 :(得分:1)
您应验证
django-民意调查
已根据您启动的命令成功安装
df = pd.read_csv('file.csv')
df['age'] = df['death'] - df['birth']
我在Windows上,如果成功,它将安装在内部
pip install --user django-polls/dist/django-polls-0.1.tar.gz
如果您只看到
C:\Users\{username}\AppData\Roaming\Python\Python37\site-packages
-->django_polls-0.1-py3.7.egg-info
-->polls
然后您应该仔细检查-->django_polls-0.1-py3.7.egg-info
是否正确(包括缩进),然后再次运行setup.cfg
如果所有操作都正确,则在提取python setup.py sdist
目录的项目(以前使用过的同一项目)内,像之前(manage.py
)一样运行服务器
polls
(请记住)D:\websiteprojects\djangotutorial01\mysite>python manage.py runserver
文件夹不再位于该文件夹中
polls
但是它仍然可以运行,因为它已成功安装在内部
D:\websiteprojects\djangotutorial01\mysite>