A
A
Alexander Vinogradov2017-02-18 17:58:20
Django
Alexander Vinogradov, 2017-02-18 17:58:20

How to get django model data via ajax?

How to get data from the model (DB) on the client?
A person presses on a picture, its viewing opens, as well as a description.
The description is a formatted text that is stored in the RichTextField field of the model. It is clear that such data will not be stored in the title attribute of the tag, and therefore they should be obtained as needed.
I don't use jQuery or otherwise. For ajax request I write in JS, for example

fetch('http://127.0.0.1:8000/main/katalog/5/', {
         method: 'GET',
         headers: {'X-Requested-With': 'XMLHttpRequest'}});

Those. here I send a specific link and get a response. And if you need to get data from the database, then you also need to write (send) the address of the link, and listen to this link in url.py? Do you still have to write something like this?
fetch('/getinfo', {
         method: 'GET',
         headers: {'X-Requested-With': 'XMLHttpRequest'}});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2017-02-18
@sim3x

write
https://docs.djangoproject.com/en/1.10/topics/http... link
to url get it If you don't understand what I wrote - go through the tutorial again

A
Alexey Sergeev, 2017-02-18
@SergeevAI

Create a url, bind to the view. It checks if request.is_ajax and sends json data. In js, you accept and output where you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question