Ad

Join() Argument Must Be Str Or Bytes, Not 'dict' -- In Django While Making A Form With Multi-Table Inheritance

I am trying to create a form for a model, that inherits from the Django auth.User class

I got this issue. Is there a better way to achieve it?

#models.py

class Teacher(User):
    #tutor = models.OneToOneField(User, on_delete=models.CASCADE, parent_link=True)
    department = models.CharField(max_length=100)
    contact_no = models.BigIntegerField()

    def __str__(self):
        return "%s %s" % (self.tutor.first_name, self.tutor.last_name)

#Forms.py
class TeacherForm(forms.ModelForm):
    class Meta:
        model = Teacher
        fields = ('username', 'first_name', 'last_name', 'email', 'contact_no', 'department')

#views.py
def new_teacher(request):
    form = TeacherForm()
    return render(request, {'form': form})

Django Version: 2.2.3
Exception Type: TypeError

Exception Value:
join() argument must be str or bytes, not 'dict'

========================================================================

The Traceback

Internal Server Error: /account/newteacher/ Traceback > (most recent call last): File > "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", > line 34, in inner > response = get_response(request) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/core/handlers/base.py", > line 115, in _get_response > response = self.process_exception_by_middleware(e, request) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/core/handlers/base.py", > line 113, in _get_response > response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/zed/PycharmProjects/untitledisbesttitle/accounts/views.py", > line 14, in new_teacher > return render(request, {'form': form}) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/shortcuts.py", > line 36, in render > content = loader.render_to_string(template_name, context, request, using=using) File > "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/template/loader.py", > line 61, in render_to_string > template = get_template(template_name, using=using) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/template/loader.py", > line 15, in get_template > return engine.get_template(template_name) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/template/backends/django.py", > line 34, in get_template > return Template(self.engine.get_template(template_name), self) File > "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/template/engine.py", > line 143, in get_template > template, origin = self.find_template(template_name) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/template/engine.py", > line 125, in find_template > template = loader.get_template(name, skip=skip) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/template/loaders/base.py", > line 18, in get_template > for origin in self.get_template_sources(template_name): File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/template/loaders/filesystem.py", > line 36, in get_template_sources > name = safe_join(template_dir, template_name) File "/home/zed/PycharmProjects/untitledisbesttitle/venv/lib/python3.6/site-packages/django/utils/_os.py", > line 32, in safe_join > final_path = abspath(join(base, *paths)) File "/usr/lib/python3.6/posixpath.py", line 94, in join > genericpath._check_arg_types('join', a, *p) File "/usr/lib/python3.6/genericpath.py", line 149, in _check_arg_types > (funcname, s.__class__.__name__)) from None TypeError: join() argument must be str or bytes, not 'dict' [06/Aug/2019 08:41:05] "GET > /account/newteacher/ HTTP/1.1" 500 120952
Ad

Answer

Here:

/home/zed/PycharmProjects/untitledisbesttitle/accounts/views.py", > line 14, in new_teacher return render(request, {'form': form})

render() second argument is supposed to be the (relative) template path:

return render(request, "yourapp/yourtemplate.html", {'form': form})

The traceback that goes with the error message is the very first thing to check to find out where an exception occurs.

Ad
source: stackoverflow.com
Ad