1

I am new to Django, I want to receive a value from the URL and then use it in a filter, but I am having this problem.

where value is a field of a model, in the view.py

class ModelNumber(generics.ListAPIView):
    permission_classes = [
        IsAuthenticated,
    ]
    serializer_class = ModelSerializer 

    def get_queryset(self):
        queryset = Model.objects.all()
        value = self.request.query_params.get('value')
        return Model.objects.filter(value = value)

at urls.py

path('model_number/(?P<receipt_ballot>\w+)$', views.ModelNumber.as_view()),

and Model.py

class Model(models.Model):                                                              
    value = models.CharField("Number Value", max_length=12, null=True)

in the url

http://192.168.0.3:8080/model_number/001-000002/

the error :

Page not found (404)

The current path, /model_number/001-000002/, didn't match any of these.

4
  • Hi Tony, Welcome to SO! can you elaborate your question further as I am finding it difficult to understand the exact issue you are facing Commented May 9, 2020 at 4:37
  • @Bernard'BetaBerlin'Parah I already edited it, at the time of sending the parameter in the url I get an error Page not found (404) Commented May 9, 2020 at 4:41
  • you need to edit your url to show the value you want to get http://192.168.0.3:8080/?model_number=001-000002/. then you can get the value in with your current code Commented May 9, 2020 at 4:46
  • another way is to use kwargs like this ...self.kwargs['param_name'] Commented May 9, 2020 at 4:48

2 Answers 2

2

You can use kwargs key defined in url to get the value of that key. Like:

    def get_queryset(self):
        # What you wanted
        value_of_url_parameter = self.kwargs.get('receipt_ballot')

        queryset = Model.objects.all()
        value = self.request.query_params.get('value')
        return Model.objects.filter(value = value)

Sign up to request clarification or add additional context in comments.

4 Comments

seriously you should stick to one answer instead of waiting for people to post comments and answers and you keep making edits. If you're not sure chill. it's not a contest
Haha!!! You think yourself a influencer!!! Thats good for you...
Cheer it out bro, Let me explain what happend... Firstly, I thought it as function based views.py so, i answered it. By, the time I editted you did a comment. So, it seems to you like I am copying you. Yes, I agree I would have pointed that out.. Thats my mistake... Have a great day mate!!!
so I was right to make the assumption, I think you should leave the answer though, I already asked OP to accept yours. I am good mate. Please put back the complete answer
1

you need to modify your url to capture the parameter

http://192.168.0.3:8080?model_number=001-000002/

class ModelNumber(generics.ListAPIView):
    permission_classes = [
        IsAuthenticated,
    ]
    serializer_class = ModelSerializer 

    def get_queryset(self):
        queryset = Model.objects.all()
        value = self.request.query_params.get('value')
        return Model.objects.filter(value = value)

notice the question mark in the url

5 Comments

You could not use /001-000002/ after query parameter
so why not point that out instead of outrightly copying it and adding it to your answer?
I am not angry..Peace mate
Also, I think value = self.request.query_params.get('value') it should be self.request.query_params.get('receipt_ballot') in this case
yes that's correct, I ignored the receipt_ballot to give a more generic answer, reason why I used value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.