Как записать данные JSON в HTML-файл в Python - PullRequest
0 голосов
/ 26 апреля 2019

У меня есть эти данные json {'latest_question_list': ((1, "What's up?", datetime.datetime(2019, 4, 19, 7, 38, 6, 449735)),)}, я хочу показать эти данные в моем html-файле. Может кто-нибудь, пожалуйста, помогите мне, как показать эти данные в нем, вот мой весь добавленный код, может кто-нибудь, пожалуйста, помогите мне решить эту проблемупроблема, также мне нужна помощь, почему я получаю результат базы данных в кортеже?

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 %}

1 Ответ

1 голос
/ 26 апреля 2019

Вы можете просто использовать двойные скобки {{}} в html, например:

{{ latest_question_list }}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...