2
votes

While trying to create a new form in Django vesrsion 2.2. I ran into this error

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

I have really tried solving the issue but I can't solve it.

These are the steps have taken..

  1. I add a new URLConf for post_new at the app level(blog folder)

    # blog/urls.py
    from django.urls import path
    from . import views
    urlpatterns = [
        path('post/new/', views.BlogCreateView.as_view(), name='post_new'),
    ]
    
  2. Then, create the view by importing a generic class called CreateView and then subclass it to create a new view called 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.

<!-- 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/

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'

Am using python3.7 with django2.2

1
Can you share the full traceback?Willem Van Onsem
I will @WillemVanOnsemgbenga wale
Did you by any change wrote template_name = 'post_new.html', (so with a comma at the end)?Willem Van Onsem
@WillemVanOnsem I don't think so but let me checkgbenga wale
@WillemVanOnsem Yes I did. Thanks I have removed it and its now workinggbenga wale

1 Answers

4
votes

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. You should remove the tailing comma.