views和urlpattern:导入问题?

时间:2016-04-20 22:44:57

标签: python django url

我关注了一个教程,但我遇到了一些问题...

我的url.py

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

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

无论如何它给了我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.pyconnection.py放在TaskManager目录中并更改url(r'^$', 'TasksManager.connection.page', name="public_connection"),中的网址(我删除了.views。),地址有效,但我的模板中的链接(html)不要:<a href="{% url 'public_connection' %}">Connection</a>并提供相同的错误import error: No module named ' TasksManager'

它也给出了这个警告:不支持对url()的字符串视图参数的支持,并且将在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'

2 个答案:

答案 0 :(得分:1)

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

  

需要__init__.py个文件才能让Python对待   目录包含包;这是为了防止   无意中具有通用名称的目录,例如string   隐藏稍后在模块搜索路径上发生的有效模块。在   最简单的情况,__init__.py可以只是一个空文件,但它可以   还执行包的初始化代码或设置__all__   变量,稍后描述

我相信你的进口应该是

from TaskManager import index 

import TaskManager

more ...

答案 1 :(得分:0)

我解决了!

我的url.py中有一个数字错误现在可以正常工作