[英]How can I solve "TypeError at /post/new/" in django
While trying to create a new form in Django vesrsion 2.2.在尝试在 Django 版本 2.2 中创建新表单时。 I ran into this error
我遇到了这个错误
TypeError at /post/new/ join() argument must be str or bytes, not 'tuple'.
/post/new/join() 参数中的类型错误必须是 str 或字节,而不是“元组”。
I have really tried solving the issue but I can't solve it.我真的尝试过解决这个问题,但我无法解决它。
These are the steps have taken..这些是已采取的步骤..
I add a new URLConf for post_new at the app level(blog folder)我在应用程序级别(博客文件夹)为 post_new 添加了一个新的 URLConf
# blog/urls.py from django.urls import path from . import views urlpatterns = [ path('post/new/', views.BlogCreateView.as_view(), name='post_new'), ]
Then, create the view by importing a generic class called CreateView and then subclass it to create a new view called BlogCreateView.然后,通过导入名为 CreateView 的通用类创建视图,然后将其子类化以创建名为 BlogCreateView 的新视图。
# blog/views.py from django.views.generic import ListView, DetailView from django.views.generic.edit import CreateView from . models import Post class BlogCreateView(CreateView): model = Post template_name = 'post_new.html' fields = '__all__'
And the last step is I create the template, which we will call post_new.html.最后一步是创建模板,我们将其命名为 post_new.html。
<!-- templates/post_new.html -->
{% extends 'base.html' %}
{% block content %}
<h1>New post</h1>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
{% endblock %}
Edited to add the full traceback编辑以添加完整的回溯
Environment:
环境:
Request Method: GET Request URL: http://127.0.0.1:8000/post/new/
请求方式:GET 请求地址: http : //127.0.0.1 : 8000 /post/ new/
Django Version: 2.2 Python Version: 3.7.3 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog'] 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.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Django 版本:2.2 Python 版本:3.7.3 已安装的应用程序:['django.contrib.admin'、'django.contrib.auth'、'django.contrib.contenttypes'、'django.contrib.sessions'、'django.contrib .messages', 'django.contrib.staticfiles', 'blog'] 安装的中间件:['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware ', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
追溯:
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\ntpath.py" in join 89. for p in map(os.fspath, paths):
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\ntpath.py” in join 89. for p in map(os.fspath, paths):
During handling of the above exception (expected str, bytes or os.PathLike object, not tuple), another exception occurred:
在处理上述异常(预期的 str、bytes 或 os.PathLike 对象,而不是元组)时,又发生了一个异常:
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\core\\handlers\\exception.py" in inner 34. response = get_response(request)
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\core\\handlers\\exception.py”在内部 34. response = get_response(request)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\core\\handlers\\base.py" in _get_response 145. response = self.process_exception_by_middleware(e, request)
_get_response 145. response = self.process_exception_by_middleware(e,要求)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\core\\handlers\\base.py" in _get_response 143. response = response.render()
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\core\\handlers\\base.py” in _get_response 143. response = response.render()
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\response.py" in render 106. self.content = self.rendered_content
渲染 106 中的文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\response.py”。 self.content = self.rendered_content
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\response.py" in rendered_content 81. template = self.resolve_template(self.template_name)
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\response.py”在render_content 81.template = self.resolve_template(self.template_name)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\response.py" in resolve_template 63. return select_template(template, using=self.using)
文件 "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\response.py" in resolve_template 63. return select_template(template, using=self.using )
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\loader.py" in select_template 42. return engine.get_template(template_name)
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\loader.py” in select_template 42. return engine.get_template(template_name)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\backends\\django.py" in get_template 34. return Template(self.engine.get_template(template_name), self)
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\backends\\django.py” in get_template 34. return Template(self.engine.get_template (模板名称),自我)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\engine.py" in get_template 143. template, origin = self.find_template(template_name)
文件"C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\engine.py" in get_template 143.template, origin = self.find_template(template_name)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\engine.py" in find_template 125. template = loader.get_template(name, skip=skip)
find_template 125 中的文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\engine.py”。跳过)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\loaders\\base.py" in get_template 18. for origin in self.get_template_sources(template_name):
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\loaders\\base.py” in get_template 18. for origin in self.get_template_sources(template_name) ):
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\loaders\\filesystem.py" in get_template_sources 36. name = safe_join(template_dir, template_name)
get_template_sources 中的文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\template\\loaders\\filesystem.py” 36.name = safe_join(template_dir, template_name)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\utils_os.py" in safe_join 32. final_path = abspath(join(base, *paths))
文件 "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django\\utils_os.py" in safe_join 32. final_path = abspath(join(base, *paths))
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\ntpath.py" in join 115. genericpath._check_arg_types('join', path, *paths)
文件 "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\ntpath.py" in join 115. genericpath._check_arg_types('join', path, *paths)
File "C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\genericpath.py" in _check_arg_types 149. (funcname, s. class . name )) from None
文件“C:\\Users\\user pc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\genericpath.py” in _check_arg_types 149. (funcname, s. class . name )) from None
Exception Type: TypeError at /post/new/ Exception Value: join() argument must be str or bytes, not 'tuple'
异常类型:TypeError at /post/new/ 异常值:join() 参数必须是 str 或字节,而不是“元组”
Am using python3.7 with django2.2我正在使用 python3.7 和 django2.2
Based on the error, you wrote something like:根据错误,您写了如下内容:
# blog/views.py
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView
from . models import Post
class BlogCreateView(CreateView):
model = Post
template_name = 'post_new.html'
, # a trailing comma
fields = '__all__'
As a result the template_name
is not a string, but a singleton tuple containing a string.因此
template_name
不是字符串,而是包含字符串的单例元组。 You should remove the tailing comma.您应该删除尾部逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.