3

I have function in view

from django.shortcuts import render
from .models import myModel

def articleTheme(request):
    if request.method == 'POST':
        article_id = request.POST['id']
        article = myModel.objects.get(id=article_id)
        theme = article.theme
        return render(request, 'theme.html', {'newTheme': theme })

now it works normal. But I have excess html. I want return json object. What I can import and return?

0

2 Answers 2

9

Use JsonResponse

from django.http import JsonResponse
..
def articleTheme(request):
    ..
    return JsonResponse({'newTheme': theme })
Sign up to request clarification or add additional context in comments.

2 Comments

And then how to access this in next view function?.
How does the template load??
0

You can return the data with HttpResponse and in JSON format like this:

data = {'newTheme': theme}
data = json.dumps(data, cls=DjangoJSONEncoder)
return HttpResponse(data)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.