문제

Im calling an endpoint in my template:

var postdata = { 'csrfmiddlewaretoken': '{{ csrf_token }}' }
$.post("reconcile/" + transaction_id, postdata);

Here is the view:

def reconcile_transaction(request, slug, id):
        society = get_object_or_404(Society, slug=slug)
        try:
            society.members.get(pk=request.user.id)
        except society.DoesNotExist:
            return HttpResponseRedirect('/')

        account = society.account
        transaction = Transaction.objects.filter(id=id)     
        print(transaction.get_stubbed_time)

I'm getting a 500 error at the print statement.

Any ideas?

도움이 되었습니까?

해결책

filter() returns a QuerySet (basically, a list of model instances). And, since there is no get_stubbed_time attribute on a QuerySet - you are getting AttributeError while trying to get get_stubbed_time attribute from a queryset. And, because of you are not running in the DEBUG mode - you are seeing 500.

You need to use get() instead:

transaction = Transaction.objects.get(id=id)     
print(transaction.get_stubbed_time)

Also, keep DEBUG=True in development - it helps a lot to quickly understand the root cause of errors. But, don't forget to turn it off in production.

Hope that helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top