In django my view.py is
import json
from django.http import HttpResponse
from django.template import Template, Context
from django.shortcuts import render_to_response
def ajax(request):
obj =[dict(a = 1,b = 2)]
jsons=json.dumps(obj)
print jsons
return render_to_response("2.html", {"obj_as_json": jsons})
I want to display value of a and b that are JSON in my template 2.html. Please help me to write the code.
I don't understand the usage of View.
Why do you want to pass JSON object as a context value while Template Rendering ?
The standard is When you do a Ajax request its response should be a JSON response i.e mimetype=application/json.
So, You should render the template normally and Convert the result into JSON and return.
e.g:
def ajax(request):
obj = {
'response': render_to_string("2.html", {"a": 1, "b": 2})
}
return HttpResponse(json.dumps(obj), mimetype='application/json')
OR
you can create a JSONResponse class Similar to HttpResponse to make it generic . e.g.
class JSONResponse(HttpResponse):
"""
JSON response
"""
def __init__(self, content, mimetype='application/json', status=None, content_type=None):
super(JSONResponse, self).__init__(
content=json.dumps(content),
mimetype=mimetype,
status=status,
content_type=content_type,
)
and use like : return JSONResponse(obj)
This has been added by default in django 1.7: https://docs.djangoproject.com/en/1.7/ref/request-response/#jsonresponse-objects
Related
What my use case is I need whole render() data into a dictionary which will be having other key values too and finally I can return it as a normal Response.
Let suppose my code is:
from django.shortcuts import render
def my_view(request):
# View code here...
return render(request, 'myapp/index.html', {
'foo': 'bar',
}, content_type='application/xhtml+xml')
Now what we are doing here is: render is basically returning a HttpResponse which we are returning.
What I need is:
Save the return response in a variable
x = render(request, 'myapp/index.html', {
'foo': 'bar',
}, content_type='application/xhtml+xml')
Then can we save it in a dictionary to return as a Response? Like this
y = {}
y = {name: 'testing', render_response: x}
return y
You cant return a plain dictionary from a view, it should return a HttpResponse object. You can return a JsonResponse from your view. Like #Daniel mentioned in comments, use render_to_string to get response in string format.
from django.template.loader import render_to_string
from django.http import JsonResponse
def my_view(request):
# View code here...
response = render_to_string('myapp/index.html', {'foo': 'bar'}, request=request)
context = {'name': 'testing', 'render_response': response}
return JsonResponse(context)
To answer your question: yes, you can. Your code, which I am rewriting here, is absolutely valid:
from django.shortcuts import render
def my_view(request):
x = render(
request,
'myapp/index.html',
{'foo': 'bar'},
content_type='application/xhtml+xml'
)
y = {name: 'testing', render_response: x}
return y
That said, you have to keep in mind that my_view is no longer a valid Django view. It is just a function that takes a request object and returns a dictionary (with an HttpResponse as one of its values).
For that reason, you will not be able to use this function in places where view functions are expected, such as in urlpatterns. A possible use of this function would be within a valid view function (which retrieves the returned HttpResponse object and returns it directly).
My code for upload in views.py is
def upload(request):
context = {}
if request.method == 'POST':
uploaded_file = request.FILES['document']
timestr = time.strftime("%Y%m%d-%H%M%S")
fs = FileSystemStorage()
uploaded_file.name = timestr+"_"+uploaded_file.name
name = fs.save(uploaded_file.name, uploaded_file)
context['url'] = fs.url(name)
return render(request, 'upload.html', context)
And my HTML webpage looks like this:
I am building an API and want to return JSON response when a user clicks on the upload button after choosing their file. I am using Django and new to web development.
You can return JSON response in three different ways in Django.
HttpResponse()
# using pure Django
from django.http import HttpResponse
return HttpResponse(json.dumps(context), content_type="application/json")
JsonResponse()
# using pure Django
from django.http import JsonResponse
return JsonResponse(context)
Response()
# using Django Rest Framework
from rest_framework.response import Response
return Response(context)
(using Django2.0)
This is my first time trying to collaborate with a frond-end developer and I am trying to serialize a Django model from a generic ListView. Even though I manage to send a JsonResponse with my objects as json, they are always a string:
"[{\"model\": \"questions.question\", \"pk\": 9535, \"fields\": {\"created\": \"2018-04-14T17:02:38.559Z\", \"modified\": \"2018-04-14T18:04:14.264Z\", \"question\": \"TEST\", \"category\": \"Rules\", \"event\": \"Beyonce\", \"answer\": \"aergaergaergaer\", \"verified\": true, \"verified_by\": [\"someotheruser\"], \"count\": 0, \"user_created\": [\"someuser\"]}}]"
the way the front-end developer solved this issue is by calling a JSON.parse(). (see: https://www.w3schools.com/js/js_json_parse.asp).
Is this the correct way to do it or should I return the objects without a string?
If I am wrong and there is a way to do this without the strings here is my view and url:
views.py:
from events.models import Event
from django.core import serializers
from django.http import JsonResponse
class EventView(LoginRequiredMixin, ListView):
login_url = '/accounts/login/'
model = Question
template_name = 'faq/faq.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
events_list = Event.objects.all()
context['events_list'] = events_list
return context
def get_queryset(self):
event = Event.objects.get(event=self.kwargs['event'])
queryset = Question.objects.filter(event=event)
return queryset
def get(self, request, *args, **kwargs):
queryset = self.get_queryset()
data = serializers.serialize("json", queryset, use_natural_foreign_keys=True)
return JsonResponse(data, status=200, safe=False)
urls.py
urlpatterns += [
path('<str:event>/', EventView.as_view(), name='event'),
]
What I also tried:
def EventRequest(request, **kwargs):
event = Event.objects.get(event=kwargs['event'])
queryset = Question.objects.filter(event=event)
data = serializers.serialize("json", queryset, use_natural_foreign_keys=True)
dump = json.dumps(data)
return HttpResponse(dump, content_type='application/json')
and:
def EventRequest(request, **kwargs):
event = Event.objects.get(event=kwargs['event'])
queryset = Question.objects.filter(event=event)
data = serializers.serialize("json", queryset, use_natural_foreign_keys=True)
return JsonResponse(data, status=200, safe=False)
Once again this could be absolutely correct and the front-end developer should just do a JSON.parse(). Please let me know, thanks!
This is normal. JSON is a string notation. It is just a format to represent JavaScript objects and arrays (Python dicts and lists) as a string.
In the frontend you'll have to use JSON.parse() to convert it into a JavaScript array (list) or object (dict).
This also holds true when you send JSON from frontend to backend. You use JSON.stringify() to covert the JS object to string. Then in the backend you convert that string to a Python object using json.loads().
Sorry for my English. I have some data from another server, but I need to output this data like JSON.
if i print response in console:
{
'responseStatus': {
'status': [],
},
'modelYear': [
1981,
1982
]
}
but, if i return this response like HttpResponse i have an error
AttributeError: 'str' object has no attribute '_meta'
this my code:
data = serializers.serialize('json', response, ensure_ascii=False)
return HttpResponse(data, content_type="application/json")
UPD:
I tried with this:
from django.http import JsonResponse
def some_view(request):
...
return JsonResponse(response, safe=False)
but have error:
Object of type 'ModelYears' is not JSON serializable
UPD:
I did like this:
import json
from django.http import JsonResponse
def some_view(request):
...
return JsonResponse(json.loads(response))
but have error:
the JSON object must be str, bytes or bytearray, not 'ModelYears'
The Django docs says the following about the serializers framework:
Django’s serialization framework provides a mechanism for “translating” Django models into other formats.
The error indicates that your variable response is a string and not an Django model object. The string seems to be valid JSON so you could use JsonResponse:
import json
from django.http import JsonResponse
# View
return JsonResponse(json.loads(response))
Replace your code with following:
from django.http import JsonResponse
def some_view(request):
...
return JsonResponse(response)
Instead of serializing and sending it via httpresponse.
This works for python 3.6 and Django 2.0
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, JsonResponse
import requests
#login_required()
def get_some_items(request):
headers = {"Authorization": "Token uydsajbkdn3kh2gj32k432hjgv4h32bhmf"}
host = "https://site/api/items"
response = requests.get(host, headers=headers)
result = JsonResponse(response.json())
return HttpResponse(result, content_type='application/json')
This is related to this question: Django return json and html depending on client python
I have a command line Python API for a Django app. When I access the app through the API it should return JSON and with a browser it should return HTML. I can use different URLs to access the different versions but how do I render the HTML template and JSON in the views.py with just one template?
To render the HTML I would use:
return render_to_response('sample/sample.html....')
But how would I do the same for JSON without putting a JSON template? (the content-type should be application/json instead of text/html)
What would determine the JSON and HTML outputs?
So in my views.py:
if something:
return render_to_response('html_template',.....)
else:
return HttpReponse(jsondata,mimetype='application/json')
I think the issue has gotten confused regarding what you want. I imagine you're not actually trying to put the HTML in the JSON response, but rather want to alternatively return either HTML or JSON.
First, you need to understand the core difference between the two. HTML is a presentational format. It deals more with how to display data than the data itself. JSON is the opposite. It's pure data -- basically a JavaScript representation of some Python (in this case) dataset you have. It serves as merely an interchange layer, allowing you to move data from one area of your app (the view) to another area of your app (your JavaScript) which normally don't have access to each other.
With that in mind, you don't "render" JSON, and there's no templates involved. You merely convert whatever data is in play (most likely pretty much what you're passing as the context to your template) to JSON. Which can be done via either Django's JSON library (simplejson), if it's freeform data, or its serialization framework, if it's a queryset.
simplejson
from django.utils import simplejson
some_data_to_dump = {
'some_var_1': 'foo',
'some_var_2': 'bar',
}
data = simplejson.dumps(some_data_to_dump)
Serialization
from django.core import serializers
foos = Foo.objects.all()
data = serializers.serialize('json', foos)
Either way, you then pass that data into the response:
return HttpResponse(data, content_type='application/json')
[Edit] In Django 1.6 and earlier, the code to return response was
return HttpResponse(data, mimetype='application/json')
[EDIT]: simplejson was remove from django, you can use:
import json
json.dumps({"foo": "bar"})
Or you can use the django.core.serializers as described above.
In Django 1.7 this is even easier with the built-in JsonResponse.
https://docs.djangoproject.com/en/dev/ref/request-response/#jsonresponse-objects
# import it
from django.http import JsonResponse
def my_view(request):
# do something with the your data
data = {}
# just return a JsonResponse
return JsonResponse(data)
In the case of the JSON response there is no template to be rendered. Templates are for generating HTML responses. The JSON is the HTTP response.
However, you can have HTML that is rendered from a template withing your JSON response.
html = render_to_string("some.html", some_dictionary)
serialized_data = simplejson.dumps({"html": html})
return HttpResponse(serialized_data, mimetype="application/json")
For rendering my models in JSON in django 1.9 I had to do the following in my views.py:
from django.core import serializers
from django.http import HttpResponse
from .models import Mymodel
def index(request):
objs = Mymodel.objects.all()
jsondata = serializers.serialize('json', objs)
return HttpResponse(jsondata, content_type='application/json')
It looks like the Django REST framework uses the HTTP accept header in a Request in order to automatically determine which renderer to use:
http://www.django-rest-framework.org/api-guide/renderers/
Using the HTTP accept header may provide an alternative source for your "if something".
You could also check the request accept content type as specified in the rfc. That way you can render by default HTML and where your client accept application/jason you can return json in your response without a template being required
from django.utils import simplejson
from django.core import serializers
def pagina_json(request):
misdatos = misdatos.objects.all()
data = serializers.serialize('json', misdatos)
return HttpResponse(data, mimetype='application/json')
Here's an example I needed for conditionally rendering json or html depending on the Request's Accept header
# myapp/views.py
from django.core import serializers
from django.http import HttpResponse
from django.shortcuts import render
from .models import Event
def event_index(request):
event_list = Event.objects.all()
if request.META['HTTP_ACCEPT'] == 'application/json':
response = serializers.serialize('json', event_list)
return HttpResponse(response, content_type='application/json')
else:
context = {'event_list': event_list}
return render(request, 'polls/event_list.html', context)
you can test this with curl or httpie
$ http localhost:8000/event/
$ http localhost:8000/event/ Accept:application/json
note I opted not to use JsonReponse as that would reserialize the model unnecessarily.
If you want to pass the result as a rendered template you have to load and render a template, pass the result of rendering it to the json.This could look like that:
from django.template import loader, RequestContext
#render the template
t=loader.get_template('sample/sample.html')
context=RequestContext()
html=t.render(context)
#create the json
result={'html_result':html)
json = simplejson.dumps(result)
return HttpResponse(json)
That way you can pass a rendered template as json to your client. This can be useful if you want to completely replace ie. a containing lots of different elements.