python - join() 参数必须是 str 或字节,而不是“元组”——在 Django 中制作报纸项目时
问题描述
我对 Django 比较陌生,并试图按照“初学者的 Django”一书从 django 制作一个模拟报纸应用程序。
我为文章创建了一个 CreateView,并在项目的文章应用程序中将其路由为“new/”,并设置了正确的模板。
现在重定向到 '/new/' 会导致这个 TypeError。
请求网址: http: //127.0.0.1 :8000/articles/new/
Django 版本:3.0.6
异常类型:TypeError
异常值:join() 参数必须是 str 或 bytes,而不是 'tuple'
异常位置:_check_arg_types 中的 c:\users\adi\anaconda3\lib\genericpath.py,第 149 行
==================================================== =======================
Traceback (most recent call last):
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
response = response.render()
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\response.py", line 105, in render
self.content = self.rendered_content
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\response.py", line 81, in rendered_content
template = self.resolve_template(self.template_name)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\response.py", line 63, in resolve_template
return select_template(template, using=self.using)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\loader.py", line 42, in select_template
return engine.get_template(template_name)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\backends\django.py", line 34, in get_template
return Template(self.engine.get_template(template_name), self)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\engine.py", line 143, in get_template
template, origin = self.find_template(template_name)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\engine.py", line 125, in find_template
template = loader.get_template(name, skip=skip)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\loaders\base.py", line 18, in get_template
for origin in self.get_template_sources(template_name):
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\template\loaders\filesystem.py", line 36, in get_template_sources
name = safe_join(template_dir, template_name)
File "C:\Users\ADI\.virtualenvs\DJANGOdEV-QAaUnZ0o\lib\site-packages\django\utils\_os.py", line 17, in safe_join
final_path = abspath(join(base, *paths))
File "c:\users\adi\anaconda3\lib\ntpath.py", line 115, in join
genericpath._check_arg_types('join', path, *paths)
File "c:\users\adi\anaconda3\lib\genericpath.py", line 149, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError:join() 参数必须是 str 或字节,而不是“元组”
createView 是:
class ArticleCreateView(CreateView):
model = Article
template_name = 'article_new.html',
fields = ('title', 'body', 'author')
并且url路由是:
path('new/', ArticleCreateView.as_view(), name='article_new'),
解决方案
你需要,
从template_name = 'article_new.html',
正确的视图应如下所示:
class ArticleCreateView(CreateView):
model = Article
template_name = 'article_new.html'
fields = ('title', 'body', 'author')
推荐阅读
- android - 如何处理列表视图中的大量数据?
- facebook - 用于未向服务器发送信息的页面的 Facebook webhook
- rest - 如何在 swagger ui 中从请求模型中排除元类
- c# - 数据安全加密
- javascript - connect 修改 prop 对象时不改变子组件的 props
- android - 从我在 android 中的布局中获取 NullPointerException
- java - Javafx mvvmfx textarea 追加
- c++ - 将对象传递给 C++ 方法
- python - ElementTree 在获取根元素时附加额外信息
- php - Amazon Lex postContent 错误“无法解码会话属性”。