[PYTHON] Get query parameters for GET requests in Django

Purpose

--A memorandum for yourself. --Organized how to get GET request parameters

I don't know if it's essentially correct, but for now I can get it.

Acquisition method

http://~~~~~~~~~~~?query_param=AAA Gets the ** query_param value ** when is specified.

Get query parameters


from rest_framework.views import APIView

class SampleView(APIView):

	def get(self, request):
		if "query_param" in request.GET:
			# query_Processing when param is specified
			param_value = request.GET.get("query_param")
		else:
			# query_Processing when param is not specified

It is better to check if there is a value.

Supplement

In get (), if the value corresponding to the key does not exist, It seems that you can specify the default value.

Like this??

Specifying the default value in get


param_value = request.GET.get(key="query_param", default="hogehoge")

reference

http://docs.djangoproject.jp/en/latest/ref/request-response.html

Recommended Posts

Get query parameters for GET requests in Django
Get query parameters for Flask GET
Get the query string (query string) in Django
[Beginner] Get from Django Query database
Get parameter values ​​in Django templates
Get the client's IP address in Django
Same-Site attribute setting for cookies in Django
Get a token for conoha in python
Models in Django
Django 1.9 for internationalization
Do an ambiguous search for mysql in Django
Forms in Django
Learning notes for the migrations feature in the Django framework (2)
How to get multiple model objects randomly in Django
List method for nested resources in Django REST framework
Learning notes for the migrations feature in the Django framework (3)
Learning notes for the migrations feature in the Django framework (1)
When you get an error in python scraping (requests)
Model changes in Django
Use ujson in requests
Get date in Python
How to use Laravel-like ORM / query builder Orator in Django
Summary of stumbling blocks in Django for the first time
Search for variables in pandas.DataFrame and get the corresponding row.
How to generate a query using the IN operator in Django
Get own process name at runtime in C / C ++ (for Linux)
Get the number of occurrences for each element in the list
Best practices for dynamically handling LINE Flex Messages in Django
[Django Learned with the Devil's Blade] How to get a query set for forward / reverse reference