Hey Devs,
First time poster here, if I am missing anything or am not following
general rules and practices feel free to let me know.
I am in the process of building an app that asks user for a url and on
submit makes post request to said url, receives json data and simply prints
it in a new view.
This part of the app works well and does the job it is supposed to do, my
only issue is that once the app has received that data, not only do I want
it printed but would also like for it to be entered into a table in my
postgres db.
I have already connected a postgres db and it has a table that is built
from my models that can receive this data.
I am struggling to understand how to actually send data to the db from the
app, the only understanding I have of entering data into db is using the
shell which does not solve my issue.
My goal is to be able to save the json data i receive from the url into
that table in my postgres db? What is the best and most efficient way to go
about accomplishing this?
I will attach some of my code so you can see what I am working with so far.
views.py
from django.shortcuts import HttpResponse, render
import urllib.request
import json
def home(request):
return render(request, 'djangoAPI/response.html')
def result(request):
url = request.GET['url']
operUrl = urllib.request.urlopen(url)
if operUrl.getcode() == 200:
data = operUrl.read()
jsonData = json.loads(data)
final = json.dumps(jsonData, indent=2, sort_keys=True)
else:
print("Error receiving data", operUrl.getcode())
return render(request, 'djangoAPI/request.html', {'result': final})
models.py
from django.db import models
class URLData(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=200)
summary = models.TextField()
description = models.TextField()
url = models.URLField(max_length=200)
rssFeed = models.TextField(blank=True, null=True)
links = models.TextField(blank=True, null=True)
def __str__(self):
return self.title
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/67f33d50-3dca-4391-a54e-ec420d833912%40googlegroups.com.