Django中的get_object_or_404问题

时间:2019-04-09 17:33:54

标签: python django

我有一个错误,我似乎无法使我的代码的这一部分正常工作。它给了我以下错误'element_id'是此函数的无效关键字参数。

起初,我发现我没有调用适当的模型,我对此进行了更改,但它似乎仍然无法正常工作。

class InputData(VoiceServiceElement): 
    """
        An element that saves user input to a line in the database.
    """

    _urls_name = 'service-development:InputData'


    ask_input_label = models.BooleanField(_('Ask the user to fill something in'), default=True)
    input_voice_label = models.ForeignKey(
        VoiceLabel,
        verbose_name = _('Ask input label'),
        help_text = _('The voice label that is played before the system asks the user to fill in the input'),
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='ask_input_label_input'
    )

    ask_confirmation = models.BooleanField(
        _('Ask the caller to confirm their input'), default=True)
    ask_confirmation_voice_label = models.ForeignKey(
        VoiceLabel,
        verbose_name = _('Ask for confirmation voice label'),
        help_text = _('The voice label that asks the user to confirm their pinput. Example: "Are you satisfied with your recording? Press 1 to confirm, or press 2 to retry."'),
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='confirmation_voice_label_input',
    )
    final_voice_label = models.ForeignKey(
        VoiceLabel,
        verbose_name = _('Final voice label'),
        help_text = _('The voice label that is played when the user has completed the input process. Example: "Thank you for your message! The message has been stored successfully."'),
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='final_voice_label_input',
    )
    input_category = models.ForeignKey(
        UserInputCategory,
        verbose_name = _('Input category'),
        help_text = _('The category under which the input will be stored in the system.'),
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='input_category_input',
    )


    _redirect = models.ForeignKey(
        VoiceServiceElement,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='%(app_label)s_%(class)s_related',
        verbose_name = _('Redirect element'),
        help_text=_("The element to redirect to after the message has been played."))

    class Meta:
        verbose_name = _('Input Element')

    @property
    def redirect(self):
        """
        Returns the actual subclassed object that is redirected to,
        instead of the VoiceServiceElement superclass object (which does
        not have specific fields and methods).
        """
        if self._redirect:
            return VoiceServiceElement.objects.get_subclass(id=self._redirect.id)
        else:
            return None

    def __str__(self):
        return "InputData: " + self.name

    def is_valid(self):
        return len(self.validator()) == 0
    is_valid.boolean = True
    is_valid.short_description = _('Is valid')

    def validator(self):
        errors = []
        errors.extend(super(InputData, self).validator())
        if not self._redirect:
            errors.append(ugettext('Input does not have a redirect element') % self.name)
        return errors

InputData模型:


2019-04-09T18:22:33.619485+00:00 heroku[router]: at=info method=GET path="/vxml/choice/35/7" host=petrichor-rain-system.herokuapp.com request_id=9c0e24be-31ec-440d-a2df-a38aa734265b fwd="217.123.34.179" dyno=web.1 connect=1ms service=308ms status=200 bytes=2180 protocol=http
2019-04-09T18:22:38.338908+00:00 app[web.1]: Internal Server Error: /vxml/InputData/33/7
2019-04-09T18:22:38.338921+00:00 app[web.1]: Traceback (most recent call last):
2019-04-09T18:22:38.338924+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
2019-04-09T18:22:38.338925+00:00 app[web.1]: response = get_response(request)
2019-04-09T18:22:38.338927+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
2019-04-09T18:22:38.338930+00:00 app[web.1]: response = self.process_exception_by_middleware(e, request)
2019-04-09T18:22:38.338984+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
2019-04-09T18:22:38.338987+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2019-04-09T18:22:38.338988+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 485, in __init__
2019-04-09T18:22:38.338990+00:00 app[web.1]: raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg)
2019-04-09T18:22:38.338996+00:00 app[web.1]: TypeError: 'element_id' is an invalid keyword argument for this function
2019-04-09T18:22:38.338997+00:00 app[web.1]: Internal Server Error: /vxml/InputData/33/7
2019-04-09T18:22:38.338998+00:00 app[web.1]: Traceback (most recent call last):
2019-04-09T18:22:38.338999+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
2019-04-09T18:22:38.339000+00:00 app[web.1]: response = get_response(request)
2019-04-09T18:22:38.339001+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
2019-04-09T18:22:38.339002+00:00 app[web.1]: response = self.process_exception_by_middleware(e, request)
2019-04-09T18:22:38.339004+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
2019-04-09T18:22:38.339005+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2019-04-09T18:22:38.339006+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 485, in __init__
2019-04-09T18:22:38.339007+00:00 app[web.1]: raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg)
2019-04-09T18:22:38.339008+00:00 app[web.1]: TypeError: 'element_id' is an invalid keyword argument for this function
2019-04-09T18:22:38.339533+00:00 app[web.1]: 10.39.172.94 - - [09/Apr/2019:20:22:38 +0200] "GET /vxml/InputData/33/7 HTTP/1.1" 500 59945 "-" "PostmanRuntime/7.6.1"
2019-04-09T18:22:38.342633+00:00 heroku[router]: at=info method=GET path="/vxml/InputData/33/7" host=petrichor-rain-system.herokuapp.com request_id=a69e1ebe-62b0-4d80-ba4b-b2f0b4bab049 fwd="217.123.34.179" dyno=web.1 connect=1ms service=48ms status=500 bytes=60194 protocol=http

有人可以帮我吗?

编辑:添加了完整的跟踪记录

from django.conf.urls import url, include

from . import views

app_name= 'service-development'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^choice/(?P<element_id>[0-9]+)/(?P<session_id>[0-9]+)$', views.choice, name='choice'),
    url(r'^message/(?P<element_id>[0-9]+)/(?P<session_id>[0-9]+)$', views.message_presentation, name='message-presentation'),
    url(r'^start/(?P<voice_service_id>[0-9]+)$', views.voice_service_start, name='voice-service'),
    url(r'^start/(?P<voice_service_id>[0-9]+)/(?P<session_id>[0-9]+)$', views.voice_service_start, name='voice-service'),
    url(r'^user/register/(?P<session_id>[0-9]+)$', views.KasaDakaUserRegistration.as_view(), name = 'user-registration'),
    url(r'^language_select/(?P<session_id>[0-9]+)$', views.LanguageSelection.as_view(), name = 'language-selection'),
    url(r'^record/(?P<element_id>[0-9]+)/(?P<session_id>[0-9]+)$', views.record, name='record'),
    url(r'^InputData/(?P<element_id>[0-9]+)/(?P<session_id>[0-9]+)$', views.InputData, name='InputData')
]

编辑2:添加了urls.py

<ns1:verifySignedDocumentResponse xmlns:ns1="http://signing.ws.comarch.gov">
    <verifySignedDocumentReturn xmlns:ns2="http://exception.ws.comarch.gov">Some string content...</verifySignedDocumentReturn>
</ns1:verifySignedDocumentResponse>

1 个答案:

答案 0 :(得分:0)

您需要更新urls.py或更改views.py文件,以使url与函数名称匹配。

views.py

def Input(request, element_id, session_id):
   ...

urls.py

app_name= 'service-development'
urlpatterns = [
    ...
    url(r'^InputData/(?P<element_id>[0-9]+)/(?P<session_id>[0-9]+)$', views.Input, name='Input')
]