[Django] A memorandum when you want to communicate asynchronously [Python3]


A memo for getting data from DB with Ajax and returning it in JSON format with Django.

First of all, in order to use Ajax with Django, if it is post, processing like the following page is required. Django Official: https://docs.djangoproject.com/ja/1.10/ref/csrf/

It is necessary to create a js file according to this.

Also, Django has a library for JSON conversion such as JSON response and serialize by default, but since it takes Model data directly and converts it to JSON, it is delicate because unnecessary data such as pk is also sent. So, this time, I thought about using the Python library to define another function for Json conversion for Ajax and control it so that it is skipped by urls.py. EX.

sample/models.py
class Sample(models.Model):
    name = models.CharField(max_length=30)
    first = models.CharField(max_length=30)
sample / ajax.py (instead of views.py)
def SampleListAjax(request): 
    objs = Sample.objects.all()
    data = [dict(name = obj.name, first = obj.first) for obj in objs]
    json = json.dumps(data)
    return HttpResponse(json, content_type="application/json")
 
sample/urls.py
class urlpatterns=[
    url(r'^sample/',ajax.SampleListAjax)
]

After that, as usual, just request the url set by Ajax with something JavaScript and get the JSON data. If you want to control with HTTP method Obtained with request ["method"]. PUT can be obtained with QueryDict.

Please let me know if there is a better way.

Recommended Posts

[Django] A memorandum when you want to communicate asynchronously [Python3]
When you want to hit a UNIX command on Python
I want to generate a UUID quickly (memorandum) ~ Python ~
When you want to filter with Django REST framework
When you want to play a game via Proxy
When you want to plt.save in a for statement
[Python] What to check when you get a Unicode Decode Error in Django
Solution when you want to use cv_bridge with python3 (virtualenv)
[Subprocess] When you want to execute another Python program in Python code
[Python] When you want to use all variables in another file
If you want to assign csv export to a variable in python
When you want to sort a multidimensional list by multiple lines
I want to build a Python environment
When you want a long line break
When you want to replace multiple characters in a string without using regular expressions in python3 series
Gist repository to use when you want to try a little with ansible
When you want to replace a column with a missing value (NaN) column by column
Python Note: When you want to know the attributes of an object
[Python] If you want to draw a scatter plot of multiple clusters
Memorandum of means when you want to make machine learning with 50 images
I want to create a window in Python
I want to make a game with Python
Python variadic memorandum when inheriting a defined class
If you want to create a Word Cloud.
When you want to update the chrome driver.
Python Note: When assigning a value to a string
How to remember when you forget a word
I want to write to a file with Python
I want to upload a Django app to heroku
How to build an environment when you want to use python2.7 after installing Anaconda3
If you want to make a TODO application (distributed) now using only Python
If you want to display values using choices in a template in a Django model
If you want to make a discord bot with python, let's use a framework
What to do when you want to receive files from a Windows client remotely
[Python3] Code that can be used when you want to cut out an image in a specific size
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
(Python) Try to develop a web application using Django
I want to write in Python! (2) Let's write a test
Settings when you want to run python-mecab with travis
A memorandum when writing experimental code ~ Logging in python
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
How to build a Django (python) environment on docker
Steps from installing Python 3 to creating a Django app
I want to work with a robot in python.
Things to note when initializing a list in Python
[Python] I want to make a nested list a tuple
Use communicate () when receiving output in a Python subprocess
Things to do when you start developing with Django
I want to run a quantum computer with Python
I want to do something in Python when I finish
What to do when you can't bind CaboCha to Python
[Python] I want to use only index when looping a list with a for statement
[Linux] When you want to search for a specific character string from multiple files
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
[AWS] What to do when you want to pip with Lambda
[Python] If you suddenly want to create an inquiry form
Introduction to Python Django (2) Win
A road to intermediate Python