Getting a TypeError

35 views
Skip to first unread message

Aakash Baranwal

unread,
Apr 16, 2019, 5:13:31 AM4/16/19
to django...@googlegroups.com
Hi Everybody,

Kindly help me out.

My views.py file is:

def video_detail_view(request):
obj = str(Video.objects.get(id=id)) 
print(obj)
context = {
"object": obj
}
return render(request, "deploy/list_view.html", context)


def video_list_view(request):
queryset = Video.objects.all()
print(queryset)
for obj in queryset:
print(obj.Video_Description, obj.videofile)
context = {
"object_list": queryset
}
return render(request, "deploy/detail_view.html", context)


The models.py file is:

class Video(models.Model):
    Video_Description= models.CharField(max_length=500)
    videofile= models.FileField(upload_to='videos/', null=True, verbose_name="")

    def __str__(self):
        return self.Video_Description + ": " + str(self.videofile)

The error I am getting is:
int() argument must be a string, a bytes-like object or a number not 'builtin_function_or_method'.

The error is in views.py file in obj = str(Video.objects.get(id=id)). 

Kindly help me out.

Thank you

Best
Aakash



laudrup

unread,
Apr 16, 2019, 5:37:01 AM4/16/19
to django...@googlegroups.com, Aakash Baranwal

Hi Aakash,

Aakash Baranwal – Tue, 16. April 2019 11:12

> The error I am getting is:int() argument must be a string, a bytes-like object
> or a number not 'builtin_function_or_method'.
>
> The error is in views.py file in obj = str(Video.objects.get(id=id)).

You need to pass an id argument to your video_detail_view() function.

The reason for the rather obscure error message you are getting is, that "id" is a builtin function in python. Try this in a python shell:

>>> type(id)
<class 'builtin_function_or_method'>

If you had named the argument something else, you'd get a less confusing error message.

Hope this helps.

Kind regards,

Kasper Laudrup

laudrup

unread,
Apr 16, 2019, 7:19:47 AM4/16/19
to django...@googlegroups.com

Hi Aakash,

Aakash Baranwal – Tue, 16. April 2019 13:12
> Hi Kasper,
> Thanks a lot for your help. I did as you suggested.
> This is the code now:
> def video_detail_view(request, pk=None): # pk == idobj =
> get_object_or_404(Video, pk=pk)print(obj)context = {"object": obj}return
> render(request, "deploy/detail_view.html", context)
> But still it is showing the error. Now the error is: Page Not Found
> Now what should I do?

First of all, please keep this thread on the mailing list instead of writing just to me.

The reason you're getting a "Page not found" error should be pretty obvious. It's because you don't have an object with that primary key (pk, probably "None"?) in your Video model.

This is all pretty basic django stuff, so I suggest you revisit the excellent Django documentation and read up on how the Django model/view stuff works.

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages