gpt4 book ai didi

python - View 和 urlpattern : problems with import?

转载 作者:太空宇宙 更新时间:2023-11-04 05:32:16 26 4
gpt4 key购买 nike

我正在学习教程,但我遇到了一些问题...

我的 url.py :

urlpatterns = [
url(r'^$', 'TasksManager.views.index.page', name="public_index"),
url(r'^connection-TasksManager$', 'TasksManager.views.connection.page',
name="public_connection"),
...
]

TasksManager 和 View 是目录,索引和连接是文件 .py,页面是这些文件中的 def。我不知道为什么教程不让我在 views.py 中写 View

无论如何,它给我一个 import error: No module named ' TasksManager'我不知道在 url.py 中导入什么...

from . import TasksManager ? (它给出错误)

import TasksManager.index ? (也许它什么都不做)

我的目录结构:

Work_Manager
|__manage.py
|__TasksManager
| |_ __init__.py
| |_admin.py
| |_apps.py
| |_models.py
| |_views.py (empty)
| |_views
| |_ __init__.py
| |_index.py
| |_connection.py
| |_...
|__Work_Manager
|_ __init__.py
|_database.db
|_settings.py
|_url.py

如果我输入 index.py , connection.py在 TaskManager 目录中并更改 url(r'^$', 'TasksManager.connection.page', name="public_connection"), 中的 url (我删除了 .views。)地址有效,但我的模板 (html) 中的链接没有:<a href="{% url 'public_connection' %}">Connection</a>并给出相同的错误 import error: No module named ' TasksManager'

它还发出警告:不推荐使用对 url() 的字符串 View 参数的支持,并将在 Django 1.10 中删除(获得 TasksManager.views.index.page)。而是传递可调用对象。

谢谢你的帮助

PS:我使用的是 Python 3.4 和 Django 1.9

完整的回溯:

    Environment:


Request Method: GET
Request URL: http://localhost:8000/

Django Version: 1.9.5
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'TasksManager']
Installed 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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Python34\Scripts\Work_Manager\Work_manager\TasksManager\templates\en\public\index.html, error at line 58
No module named ' TasksManager' 48 : {% for city in array_city %}
49 : <li>
50 : {{ city }}
51 : </li>
52 : {% endfor %}
53 : </ul>
54 :
55 : <article>
56 : Hello world ! <br />
57 : <!--<a href="connection">Connection</a>-->
58 : <a href=" {% url 'public_connection' %} ">Connection</a>
59 : <a href="{% url 'create_developer' %}">Create developer</a>
60 : <a href="{% url 'create_supervisor' %}">Create supervisor</a>
61 : <a href="{% url 'create_project' %}">Create project</a>
62 : </article>
63 : {% endblock %}
64 :

Traceback:

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\urlresolvers.py" in get_callable
115. mod = import_module(mod_name)

File "C:\Python34\lib\importlib\__init__.py" in import_module
109. return _bootstrap._gcd_import(name[level:], package, level)

During handling of the above exception (No module named ' TasksManager'), another exception occurred:

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python34\Scripts\Work_Manager\Work_manager\TasksManager\views\index.py" in page
39. return render(request, 'en/public/index.html', locals())

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\shortcuts.py" in render
67. template_name, context, request=request, using=using)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\loader.py" in render_to_string
97. return template.render(context, request)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\backends\django.py" in render
95. return self.template.render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in render
206. return self._render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in _render
197. return self.nodelist.render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in render
992. bit = node.render_annotated(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in render_annotated
959. return self.render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\loader_tags.py" in render
173. return compiled_parent._render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in _render
197. return self.nodelist.render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in render
992. bit = node.render_annotated(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in render_annotated
959. return self.render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\loader_tags.py" in render
69. result = block.nodelist.render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in render
992. bit = node.render_annotated(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\base.py" in render_annotated
959. return self.render(context)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\template\defaulttags.py" in render
499. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\urlresolvers.py" in reverse
600. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
448. self._populate()

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\urlresolvers.py" in _populate
334. lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\urlresolvers.py" in callback
255. self._callback = get_callable(self._callback_str)

File "C:\Python34\lib\functools.py" in wrapper
448. result = user_function(*args, **kwds)

File "c:\Python34\Scripts\Work_Manager\myvenv\lib\site-packages\django\core\urlresolvers.py" in get_callable
121. if submod and not module_has_submodule(import_module(parentmod), submod):

File "C:\Python34\lib\importlib\__init__.py" in import_module
109. return _bootstrap._gcd_import(name[level:], package, level)

Exception Type: ImportError at /
Exception Value: No module named ' TasksManager'

最佳答案

您的文件夹中似乎没有__init__.py 文件。这可以只是一个空文件。

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later

我相信你的导入应该是

from TaskManager import index 

import TaskManager

more ...

关于python - View 和 urlpattern : problems with import?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com