python join()参数必须是string、bytes或os.PathLike对象,而不是Django中的'NoneType'

bttbmeg0  于 2023-09-29  发布在  Python
关注(0)|答案(1)|浏览(121)

我的Django应用程序遇到了一个问题。问题是当我点击Add a Record时,应用程序返回错误:join() argument must be str, bytes, or os.PathLike object, not 'NoneType',但它不应该,因为我没有在我的代码中做任何加入。此视图为:

def add_job_record(request):
    add_form = AddJobTrackerForm(request.POST or None)
    if request.user.is_authenticated:
        if request.method == "POST":
            if add_form.is_valid():
                add_job_record = add_form.save()
                messages.success(request, 'Job Tracker added')
                return redirect('core:home')
        return render(request, 'applications/add_record.html', {'add_form': add_form})
    else:
        messages.danger(request, "You must be logged in")
        return redirect('core:home')

我在这里发现了一个帖子(我忘记保存链接),其中的答案是从我的代码中删除{'add_form': add_form},但当我删除它时,表单不会显示在页面上
回溯是:

File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\project\apps\applications\views.py", line 35, in add_job_record
    return render(request, 'applications/add_record.html', {'add_form': add_form})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\shortcuts.py", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\test\utils.py", line 112, in instrumented_test_render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loader_tags.py", line 157, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\test\utils.py", line 112, in instrumented_test_render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1070, in render
    return render_value_in_context(output, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1047, in render_value_in_context
    value = str(value)
            ^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\utils.py", line 75, in render
    return mark_safe(renderer.render(template, context))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\renderers.py", line 29, in render
    return template.render(context, request=request).strip()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\test\utils.py", line 112, in instrumented_test_render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loader_tags.py", line 208, in render
    return template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 177, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\test\utils.py", line 112, in instrumented_test_render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1070, in render
    return render_value_in_context(output, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\base.py", line 1047, in render_value_in_context
    value = str(value)
            ^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\utils\html.py", line 420, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
                                           ^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\boundfield.py", line 34, in __str__
    return self.as_widget()
           ^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\boundfield.py", line 107, in as_widget
    return widget.render(
           ^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\widgets.py", line 281, in render
    return self._render(self.template_name, context, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\widgets.py", line 286, in _render
    return mark_safe(renderer.render(template_name, context))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\renderers.py", line 28, in render
    template = self.get_template(template_name)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\forms\renderers.py", line 34, in get_template
    return self.engine.get_template(template_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\backends\django.py", line 33, in get_template
    return Template(self.engine.get_template(template_name), self)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\engine.py", line 175, in get_template
    template, origin = self.find_template(template_name)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\engine.py", line 157, in find_template
    template = loader.get_template(name, skip=skip)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loaders\cached.py", line 57, in get_template
    template = super().get_template(template_name, skip)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loaders\base.py", line 17, in get_template
    for origin in self.get_template_sources(template_name):
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loaders\cached.py", line 70, in get_template_sources
    yield from loader.get_template_sources(template_name)
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\template\loaders\filesystem.py", line 35, in get_template_sources
    name = safe_join(template_dir, template_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\DEV\job-tracker\venv\Lib\site-packages\django\utils\_os.py", line 17, in safe_join
    final_path = abspath(join(base, *paths))
                         ^^^^^^^^^^^^^^^^^^
  File "<frozen ntpath>", line 147, in join
  File "<frozen genericpath>", line 152, in _check_arg_types
TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
[23/Sep/2023 20:17:59] "GET /tracker/add-record/ HTTP/1.1" 500 332790
zsohkypk

zsohkypk1#

通过使用CBV实现create函数解决了我自己的问题

class ApplicationCreateView(LoginRequiredMixin, CreateView):
    model = Application
    fields = [
        'name',
        'email',
        'company_name',
        'position',
        'rate',
        'status',
        'notes'
    ]

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super().form_valid(form)

相关问题