load json data for the first time request and to display the same in home page

  • Last Update :
  • Techknowledgy :

views.py

def articles(request):
   context {
      'articles': ['a1', 'a2', 'a3']
   }
return render(request, 'articles.html', context)

articles.html

{% verbatim %}
<div id="app">
    <ul>
     <li v-for="a in articles">{{ a }}</li>
    </ul>
</div>
{% endverbatim %}

<script>
    new Vue({
        el : "#app",
        data : function(){
            return {
                articles : {{ articles | safe }}
            }
        }
    })
</script>

Suggestion : 2

I am using Vue.js for the first time. I need to serialize the objects of django ,I have tried to display serialized json data in html its working fine there, ,The verbatim tag to stop Django from rendering the contents of this block tag since Vue uses the same interpolating symbols.,Now , how to intialize json data in vue instance and to access in html using v-repeat attribute.

views.py

 def articles(request):
    model = News.objects.all() # getting News objects list
 modelSerialize = serializers.serialize('json', News.objects.all())
 random_generator = random.randint(1, News.objects.count())
 context = {
    'models': modelSerialize,
    'title': 'Articles',
    'num_of_objects': News.objects.count(),
    'random_order': random.randint(1, random_generator),
    'random_object': News.objects.get(id = random_generator),
    'first4rec': model[0: 4],
    'next4rec': model[4: ],
 }
 return render(request, 'articles.html', context)

views.py

def articles(request):
   context {
      'articles': ['a1', 'a2', 'a3']
   }
return render(request, 'articles.html', context)

articles.html

{% verbatim %}
<div id="app">
    <ul>
     <li v-for="a in articles">{{ a }}</li>
    </ul>
</div>
{% endverbatim %}

<script>
    new Vue({
        el : "#app",
        data : function(){
            return {
                articles : {{ articles | safe }}
            }
        }
    })
</script>

Suggestion : 3

06/07/2022

Here's an example of JSON text:

[{
      "name": "John",
      "skills": ["SQL", "C#", "Azure"]
   },
   {
      "name": "Jane",
      "surname": "Doe"
   }
]

In the following example, the query uses both relational and JSON data (stored in a column named jsonCol) from a table:

SELECT Name, Surname,
JSON_VALUE(jsonCol, '$.info.address.PostCode') AS PostCode,
   JSON_VALUE(jsonCol, '$.info.address."Address Line 1"') + ' ' +
   JSON_VALUE(jsonCol, '$.info.address."Address Line 2"') AS Address,
   JSON_QUERY(jsonCol, '$.info.skills') AS Skills
FROM People
WHERE ISJSON(jsonCol) > 0
AND JSON_VALUE(jsonCol, '$.info.address.Town') = 'Belgrade'
AND Status = 'Active'
ORDER BY JSON_VALUE(jsonCol, '$.info.address.PostCode')

If you must modify parts of JSON text, you can use the JSON_MODIFY (Transact-SQL) function to update the value of a property in a JSON string and return the updated JSON string. The following example updates the value of a property in a variable that contains JSON:

DECLARE @json NVARCHAR(MAX);
SET @json = '{"info": {"address": [{"town": "Belgrade"}, {"town": "Paris"}, {"town":"Madrid"}]}}';
SET @json = JSON_MODIFY(@json, '$.info.address[1].town', 'London');
SELECT modifiedJson = @json;

In the following example, the second object in the array has sub-array representing person skills. Every sub-object can be parsed using additional OPENJSON function call:

DECLARE @json NVARCHAR(MAX);
SET @json = N '[   {
"id": 2,
"info": {
   "name": "John",
   "surname": "Smith"
},
"age": 25
}, {
"id": 5,
"info": {
   "name": "Jane",
   "surname": "Smith",
   "skills": ["SQL", "C#", "Azure"]
},
"dob": "2005-11-04T12:00:00"
}
]
';

SELECT id, firstName, lastName, age, dateOfBirth, skill
FROM OPENJSON(@json)
WITH(
   id INT 'strict $.id',
   firstName NVARCHAR(50)
   '$.info.name',
   lastName NVARCHAR(50)
   '$.info.surname',
   age INT,
   dateOfBirth DATETIME2 '$.dob',
   skills NVARCHAR(MAX)
   '$.info.skills'
   AS JSON
)
OUTER APPLY OPENJSON(skills)
WITH(skill NVARCHAR(8)
   '$');

The following example uses PATH mode with the FOR JSON clause:

SELECT id, firstName AS "info.name", lastName AS "info.surname", age, dateOfBirth AS dob
FROM People
FOR JSON PATH;

Suggestion : 4

Some servers do allow cross-origin requests through a mechanism called CORS (Cross-origin resource sharing), which uses headers in an HTTP request to ask for and receive permission. CORS is server specific.,One useful HTTP request your web app can make is a GET request for a data file served from the same origin as the app. The example below reads a data file called portmanteaux.json that contains a JSON-formatted list of words. When you click the button, the app makes a GET request of the server and loads the file.,In this example, the full JSON string is hard coded into the Dart code, but it could be created by the form itself or read from a static file or fetched from a server. An example later in this page shows how to dynamically fetch JSON data from a file that is co-located with the code for the app.,Most HTTP requests in a web browser are simple GET requests asking for the contents of a page. However, the HTTP protocol allows for other types of requests, such as POST for sending data from the client.

import 'dart:convert';
const jsonDataAsString = '''{
  "favoriteNumber": 73,
  "valueOfPi": 3.141592,
  "chocolate": true,
  "horoscope": "Cancer",
  "favoriteThings": ["monkeys", "parrots", "lattes"]
}''';

Map<String, dynamic> jsonData =
    json.decode(jsonDataAsString) as Map<String, dynamic>;
Future<void> makeRequest(Event _) async {
  const path = 'https://dart.dev/f/portmanteaux.json';
  final httpRequest = HttpRequest();
  httpRequest
    ..open('GET', path)
    ..onLoadEnd.listen((e) => requestComplete(httpRequest))
    ..send('');
}
httpRequest.send('');
[
   "portmanteau", "fantabulous", "spork", "smog",
   "spanglish", "gerrymander", "turducken", "stagflation",
   "bromance", "freeware", "oxbridge", "palimony", "netiquette",
   "brunch", "blog", "chortle", "Hassenpfeffer", "Schnitzelbank"
]