gpt4 book ai didi

python - 我怎样才能解决 Django 中的 "TypeError at/post/new/"

转载 作者:行者123 更新时间:2023-11-28 22:10:29 25 4
gpt4 key购买 nike

尝试在 Django 版本 2.2 中创建新表单时。我遇到了这个错误

TypeError at /post/new/ join() argument must be str or bytes, not 'tuple'.

我确实尝试过解决问题,但我无法解决。

这些是已经采取的步骤..

  1. 我在应用级别(博客文件夹)为 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'),
    ]
  2. 然后,通过导入一个名为 CreateView 的通用类来创建 View ,然后将其子类化以创建一个名为 BlogCreateView 的新 View 。

    # 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 %}

编辑以添加完整的回溯

Environment:

Request Method: GET Request URL: 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']

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):

During handling of the above exception (expected str, bytes or os.PathLike object, not tuple), another exception occurred:

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)

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)

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()

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

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)

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)

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)

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)

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)

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)

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):

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)

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))

File "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

Exception Type: TypeError at /post/new/ Exception Value: join() argument must be str or bytes, not 'tuple'

我正在使用 python3.7 和 django2.2

最佳答案

根据错误,你写了类似这样的东西:

# 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'<b>,</b> # a trailing comma
fields = '__all__'

因此 template_name 不是一个字符串,而是一个包含字符串的单例元组。您应该删除结尾的逗号。

关于python - 我怎样才能解决 Django 中的 "TypeError at/post/new/",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56630585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com