尝试在Django vesrsion 2.2中创建新表单时。我遇到了这个错误
/ p / post / new / join()参数中的TypeError必须是str或字节,而不是'tuple'。
我真的尝试解决了这个问题,但是我解决不了。
这些是已经采取的步骤。
我在应用程序级别(博客文件夹)为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'),
]
然后,通过导入一个名为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__'
最后一步是创建模板,我们将其称为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 %}
已编辑以添加完整的回溯
环境:
请求方法:GET请求URL:http://127.0.0.1:8000/post/new/
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']
跟踪:
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ ntpath.py”加入 89.对于map中的p(os.fspath,路径):
在处理以上异常期间(预期的str,字节或 os.PathLike对象,而不是元组),发生了另一个异常:
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ exception.py“ 在内部 34. response = get_response(request)
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py“ 在_get_response中 145. response = self.process_exception_by_middleware(e,request)
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py“ 在_get_response中 143. response = response.render()
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ template \ response.py“ 在渲染 106. self.content = self.rendered_content
文件“ 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)
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ template \ response.py“ 在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 \ loader.py“ 在select_template中 42.返回engine.get_template(template_name)
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ template \ backends \ django.py“ 在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 \ engine.py“ 在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“ 在find_template中 125. template = loader.get_template(name,skip = skip)
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ template \ loaders \ base.py“ 在get_template中 18.对于self.get_template_sources(template_name)的来源:
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ template \ loaders \ filesystem.py“ 在get_template_sources中 36.名称= safe_join(template_dir,template_name)
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ utils_os.py“ 在safe_join 32. final_path = abspath(join(base,* paths))
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ ntpath.py”加入 115.genericpath._check_arg_types('join',path,* paths)
文件“ C:\ Users \ user pc \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ genericpath.py”中 _check_arg_types 149.(funcname,s。 class 。 name ))来自无
异常类型:/ post / new /处的TypeError异常值:join() 参数必须是str或字节,而不是'tuple'
Am使用python3.7和django2.2
答案 0 :(得分:3)
基于该错误,您编写了如下内容:
# 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__'
结果,template_name
不是字符串,而是包含字符串的单例元组。您应该删除尾部逗号。