我是Django的新手。
当我尝试从api.py文件中包含url时,它会返回错误。
这是文章app文件夹中的api.py文件
from tastypie.resources import ModelResource
from tastypie.constants import ALL
from article.models import Article
class ArticleResource(ModelResource):
class Meta:
querySet = Article.objects.all()
resource_name = 'article'
这是文章app文件夹中的urls.py文件
from django.conf.urls import url
from article.views import *
from .api import ArticleResource
from django.conf.urls import include
article_resource = ArticleResource()
urlpatterns = [
url(r'^api/',include('article_resource.urls'))
]
我得到的错误就是这个
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0470C9C0>
Traceback (most recent call last):
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\Vineeth\Desktop\programs\mydjango\mysite\mysite\urls.py", line 29, in <module>
url(r'^articles/',include('article.urls')),
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\Vineeth\Desktop\programs\mydjango\mysite\article\urls.py", line 16, in <module>
url(r'^api/',include('article_resource.urls'))
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Vineeth\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'article_resource'
答案 0 :(得分:1)
从include中删除引号将解决问题
urlpatterns = [
url(r'^api/',include(article_resource.urls))
]
注意:Django include
函数使用python的import_module
从字符串中导入模块,但在这里,article_resource
仅在本地范围内可用。