[英]Attribute Error when Running Django Rest API
I have created a simple project called djangowallet and inside that project, I have created an app called wallet with a model wallet and just one field called raddress .我创建了一个名为djangowallet的简单项目,在该项目中,我创建了一个名为wallet的应用程序,其中包含一个 model钱包和一个名为raddress的字段。
I have created the serializer, views and URLs and when I runserver it gives the following error.我已经创建了序列化程序、视图和 URL,当我运行服务器时它会出现以下错误。
PS C:\Users\Ahmed\djangowallet> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 438, in check
all_issues = checks.run_checks(
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\registry.py", line 77, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 448, in check
for pattern in self.url_patterns:
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 634, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 627, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\Ahmed\djangowallet\djangowallet\urls.py", line 22, in <module>
path('wallet/', include('wallet.urls'))
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\xml\etree\ElementInclude.py", line 128, in include
_include(elem, loader, base_url, max_depth, set())
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\lib\xml\etree\ElementInclude.py", line 136, in _include
if e.tag == XINCLUDE_INCLUDE:
AttributeError: 'str' object has no attribute 'tag'
This is my URL file in the project directory.这是我在项目目录下的URL文件。
from xml.etree.ElementInclude import include
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('wallet/', include('wallet.urls'))
]
This is the URL file in the wallet app这是钱包应用程序中的 URL 文件
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'wallet', views.WalletViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', include(router.urls)),
]
Seems you are looking to useinclude
..[Django-doc] so just change:似乎您正在寻找使用
include
..[Django-doc]所以只需更改:
from xml.etree.ElementInclude import include
to:到:
from django.urls import include
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.