简体   繁体   中英

HTTP request function, indicates that there is no model attribute - DJango

I've implemented a model and a view function.

The problem is that the function, which takes the http resquest, tells me that an attribute of the model does not exist. And I don't know what could be happening.

views.py

def home (request , data=None):
    #item.objects.filter(radius)
    items_area_location =item.objects.filter(radius<data)
    response = serialize('json', items_area_location)
    return HttpResponse(response) 

Models.py

class item (models.Model):
    certificate=models.ImageField(default=None) 
    provider = models.ForeignKey(serviceProvider, on_delete=models.CASCADE)
    radius = models.FloatField(default=None)
    description= models.TextField(blank = True)
    hour_init = models.TimeField()
    hour_end = models.TimeField()

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/<int:data>/', home, name='home'),
]

This is what I get when there is a GET resquest with domain/home/somenumber:

name 'radius' is not defined

> NameError at /home/10/ name 'radius' is not defined Request
> Method:   GET Request URL:    http://127.0.0.1:8000/home/10/ Django
> Version:  2.2.12 Exception Type:  NameError Exception Value:   name
> 'radius' is not defined Exception
> in home, line 15 Python Executable:   /usr/bin/python3 Python
> Version:  3.8.5 Python Path:  
> ['/home/julian/Documentos/Programación/CallServiceBackEnd', 
> '/usr/lib/python38.zip',  '/usr/lib/python3.8', 
> '/usr/lib/python3.8/lib-dynload', 
> '/home/julian/.local/lib/python3.8/site-packages', 
> '/usr/local/lib/python3.8/dist-packages', 
> '/usr/lib/python3/dist-packages'] Server time:    Tue, 1 Dec 2020
> 13:46:46 +0000

Not that:

items_area_location =item.objects.filter(radius<data)

but rather:

items_area_location =item.objects.filter(radius__lt=data)

Is your urls.py in an app or in the home directory? Then I can help you more.

But I can help you with the views.py

To make sure, first, did you import the models?

If you did then you need to fix this

items_area_location =item.objects.filter(radius<data)

I think this is wrong syntax. And change it to this

items_area_location =item.objects.filter(radius)

Try this

Okay, then you can either edit the list or dictionary to get what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM