0

I have this json data {'latest_question_list': ((1, "What's up?", datetime.datetime(2019, 4, 19, 7, 38, 6, 449735)),)} , i want to show this data in my html file can anyone please help me how to show this data in it, here is my whole code added, can anyone please help me to resolve this issue, also i need help why i am getting database result in tuple ?

models.py

import datetime
from django.utils import timezone
from django.db import connection
from django.db import models


class Question():
    @classmethod
    def get_poll_question(cls):
        with connection.cursor() as cursor:
            db_table = "polls_question"
            cursor.execute('SELECT * FROM '+db_table)
            allquestion = cursor.fetchall()
            return allquestion

class Choice():
    def get_options(cls):
        with connection.cursor() as cursor:
            db_table = "polls_choice"
            cursor.execute("SELECT * FROM "+ db_table+" WHERE question_id = '1' ")
            choice_text = cursor.fetchall();
            return choice_text

views.py

from django.shortcuts import render

# Create your views here.


from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from .models import Choice, Question
from django.urls import reverse
from django.shortcuts import get_object_or_404, render



def index(request):
    latest_question_list = Question.get_poll_question()
    context = {'latest_question_list': latest_question_list}
    print(context)
    return render(request, 'polls/index.html', context)

index.html

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
6
  • 1
    i think your json data is not standard ! can you change that or it should be just like that ? Commented Apr 26, 2019 at 5:28
  • it is not a json, it has python tuple Commented Apr 26, 2019 at 6:24
  • @shourav how to conver tuple in associate array ? Commented Apr 26, 2019 at 6:57
  • can you update the question with the definition of Question.get_poll_question()? Commented Apr 26, 2019 at 6:59
  • Sure let me update Commented Apr 26, 2019 at 7:03

1 Answer 1

1

You can simply use the double brackets {{}} in the html, like this:

{{ latest_question_list }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.