1

My url pattern :

path('business/average/turnover/<str:start_date>/<str:end_date>/', views.AverageTurnover.as_view())

when go to the url it shows something like this :

http://127.0.0.1:8000/business/average/turnover/2019-01-1/2019-12-12/

but i want it to be like this :

http://127.0.0.1:8000/business?start_date=adfadf&end_date=xsdfa

How to do this.

Note: I have no view function is rendering this url but when i am directly hitting this url then it goes to views like this.

views.py

class AverageTurnover(APIView):
    '''Average Turn Over'''

    def get(self, request, start_date, end_date):
        avg_turnover = BusinessShareInfo.objects.filter(Date__range=(start_date, end_date)).aggregate(Avg('Turnover'))
        return Response(avg_turnover)        


2
  • what about views.py? Commented May 26, 2020 at 6:50
  • Biplove updating Commented May 26, 2020 at 6:51

2 Answers 2

1

All you have to do is to receive GET request in your view. This is how you can achieve what you want but still I would recommend going that way it is cleaner.

Nevertheless here are the changes you will need to do.

  • Change your url to
path('business/', views.AverageTurnover.as_view())
  • Change your views.py so it can handle GET requests (you can learn about them here)

  • Also remember to add form in template. (you can learn about them here)

Note: Remember to use GET request method as it passes the data in the url and this is what you want. You do not want to use POST request method as it does not passes data in url (a lot cleaner)

You are doing it wrong, I suggest you to read the resources I mentioned. The way you taking arguments will require you have this type of url,

http://127.0.0.1:8000/business/average/turnover/2019-01-1/2019-12-12/

If you will use GET method properly you can change it to,

http://127.0.0.1:8000/business?start_date=adfadf&end_date=xsdfa
Sign up to request clarification or add additional context in comments.

1 Comment

my get request function is taking two arguments.Sir please see views.py i have updated.
0

class AverageTurnover(generics.RetriveAPIView): '''Average Turn Over'''

def get(self, request, *args, **kwargs):
    start_date = self.request.query_params.get('start_date')
    end_date = self.request.query_params.get('end_date')
    avg_turnover = BusinessShareInfo.objects.filter(Date__range=(start_date, end_date)).aggregate(Avg('Turnover'))
    return Response(avg_turnover)    

Then use the url @Hisham__Pak and when invoking this api just pass the query params as you mentioned.

http://127.0.0.1:8000/business?start_date=adfadf&end_date=xsdfa

1 Comment

I will see and let you know.Thanks

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.