(类型错误:期望的字符串或类似字节的对象)在Django中调用函数时

时间:2017-02-18 22:29:36

标签: python django function error-handling typeerror

我尝试创建一个网站,通过表单获取用户文本输入,并仅返回输入中的问题。我希望每次提交一些输入时都将初始用户输入和问题保存到我的数据库中。基本上我已经完成了所有工作,除了每次测试表单中的文本提交时我都会遇到这个TypeError。以下是错误的详细信息:

Traceback:

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/joshuablew/Projects/HW/HWapp/v1/views.py" in user_text_view
  20.             user_questions = Question_Init(user_input_obj)

File "/Users/joshuablew/Projects/HW/HWapp/v1/function1.py" in Question_Init
  23.   textList = sent_tokenize(text1)

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/__init__.py" in sent_tokenize
  94.     return tokenizer.tokenize(text)

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/punkt.py" in tokenize
  1237.         return list(self.sentences_from_text(text, realign_boundaries))

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/punkt.py" in sentences_from_text
  1285.         return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)]

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/punkt.py" in span_tokenize
  1276.         return [(sl.start, sl.stop) for sl in slices]

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/punkt.py" in <listcomp>
  1276.         return [(sl.start, sl.stop) for sl in slices]

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/punkt.py" in _realign_boundaries
  1316.         for sl1, sl2 in _pair_iter(slices):

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/punkt.py" in _pair_iter
  310.     prev = next(it)

File "/Users/joshuablew/Projects/HW/env/lib/python3.6/site-packages/nltk/tokenize/punkt.py" in _slices_from_text
  1289.         for match in self._lang_vars.period_context_re().finditer(text):

Exception Type: TypeError at /input/
Exception Value: expected string or bytes-like object

这是我在文本上运行的函数:

import nltk

from nltk.tokenize import sent_tokenize

from v1 import views


def Question_Init(user_input_obj):

    Beginning_Question_Prompts = ("Who","Whom","What","Where","When","Why","Which",
    "Whose","How","Was","Were","Did","Do","Does","Is")
    Ending_Question_Prompts = ("?",":","...")
    questions = []

    text1 = user_input_obj

    textList = sent_tokenize(text1)

    for sentence in textList:
        if sentence.startswith(Beginning_Question_Prompts):
            questions.append(sentence)

        if sentence.endswith(Ending_Question_Prompts):
            questions.append(sentence)

    return questions

以下是我的观点,我用来将输入保存到我的数据库并在输入上运行我的函数。

def text_input(request):
    form = forms.UserTextForm()

    if request.method == 'POST':
        request:
        form = forms.UserTextForm(request.POST)

        if form.is_valid():
            user_input = request.POST.get('user_input', '')
            user_input_obj = models.UserText(user_input = user_input)
            user_questions = Question_Init(user_input_obj)
            user_questions.save()

            print(user_questions)


    else:
        form = forms.UserTextForm()

    return render(request, 'text_input_form.html', {'form': form})

我的模特:

class UserText(models.Model):
    user_input = models.CharField(max_length=2000)

我不明白这个错误告诉我我需要做什么。我是否需要将用户输入转换为字符串或字节类型的东西?我该怎么做呢?我还读到我可能需要在模型中使用 unicode ()方法做一些事情。我不熟悉如何在这种情况下使用unicode。 unicode在这里有什么意义吗?谢谢你的帮助,我很感激。

1 个答案:

答案 0 :(得分:0)

您将整个UserInput对象传递给nlkt函数,因为错误状态只是期望一个字符串。你可以通过传递相关字段来解决这个问题:

text1 = user_input_obj.user_input

但老实说我很困惑你为什么要首先创建该模型 - 将request.POST.get(...)的结果传递给Question_Init函数会更简单。

另请注意,该函数会返回一个句子列表,然后您可以调用save - 这不会起作用,因为列表没有保存方法。不确定你要保存的是什么。