import jsonresponse django
from django.http import JsonResponse
return JsonResponse({'key':'value'})
import jsonresponse django
from django.http import JsonResponse
return JsonResponse({'key':'value'})
django import json
"""
Import json data from URL to Datababse
"""
import requests
import json
from import_data.models import Movie #Import your model here
from django.core.management.base import BaseCommand
from datetime import datetime
IMPORT_URL = 'https://jsonplaceholder.typicode.com/photos' # URL to import from
class Command(BaseCommand):
def import_movie(self, data):
title = data.get('title', None)
url = data.get('url', None);
release_year = datetime.now()
try: #try and catch for saving the objects
movie, created = Movie.objects.get_or_create(
title=title,
url=url,
release_year=release_year
)
if created:
movie.save()
display_format = "\nMovie, {}, has been saved.
print(display_format.format(movie))
exceptExceptionas ex:
print(str(ex))
msg = "\n\nSomething went wrong saving this movie: {}\n{}".format(title, str(ex))
print(msg)
def handle(self, *args, **options):
"""
Makes a GET request to the API.
"""
headers = {'Content-Type': 'application/json'}
response = requests.get(
url=IMPORT_URL,
headers=headers,
)
response.raise_for_status()
data = response.json()
for data_object in data:
self.import_movie(data_object)
jsonresponse django
from django.http import JsonResponse
return JsonResponse({'foo':'bar'})
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us