从同一模板中的外部URL渲染JSON

时间:2018-08-02 22:11:52

标签: python jquery django

我需要呈现从另一个视图返回的JSON信息。这是我的代码,我尝试使用HttpResponseRedirect,但无法执行。 当我单击该按钮时,我想在同一页面的第二个div(col.sm.9)上呈现utils返回到“ estadistica”视图的JSON,希望不会重新显示所有页面

模板

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Ganadero</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
    /* Set height of the grid so .sidenav can be 100% (adjust if needed) */
    .row.content {height: 1500px}

    /* Set gray background color and 100% height */
    .sidenav {
      background-color: #f1f1f1;
      height: 100%;
    }

    /* Set black background color, white text and some padding */
    footer {
      background-color: #555;
      color: white;
      padding: 15px;
    }

    /* On small screens, set height to 'auto' for sidenav and grid */
    @media screen and (max-width: 767px) {
      .sidenav {
        height: auto;
        padding: 15px;
      }
      .row.content {height: auto;} 
    }
  </style>
</head>
<body>

<div class="container-fluid">
  <div class="row content">
    <div class="col-sm-3 sidenav">
      <h4>Ganado</h4>
      <form method="POST">
        {% csrf_token %}
        <label class="row">
        Animal: {{ form.animal }}
        </label>
        <label class="row">
        Ciudad: {{ form.ciudad }}
        </label>
        <label class="row">
        Feria: {{ form.feria }}
        </label>
      </form>
      <div class="input-group row">
        <span class="input-group-btn">
          <button class="btn btn-default" type="button">
            <label>Buscar:</label><span class="glyphicon glyphicon-search"></span>
          </button>
        </span>
      </div>
    </div>

    <div class="col-sm-9">
      <div id="json_va_aqui">
        {{ form.values }}
      </div>
    </div>
  </div>
</div>

<footer class="container-fluid">
  <p>Footer Text</p>
</footer>

</body>
</html>

views.py

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from collections import OrderedDict
from .models import *
from .utils import *
from .forms import *
from datetime import datetime, timedelta
import simplejson as json
from itertools import filterfalse
from django import db
import heapq

def index(request):
    if request.method == 'POST':
        form = AnimalForm(request.POST)
        if form.is_valid():
            animal = form.cleaned_data['animal']
            ciudad = form.cleaned_data['ciudad']
            feria = form.cleaned_data['feria']
            result_json = estadistica(request, ciudad, feria, animal)
            return HttpResponse(result_json, content_type='application/json')
    else:
        form = AnimalForm()

    return render(request, 'index.html', {'form': form})


def estadistica(request,ciudad, feria, animal):

    result = estadistica_basic(request, ciudad, feria, animal)

    result = json.dumps(result)

    return result

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<slug:ciudad>/<slug:feria>/<slug:animal>',
        views.estadistica,
        name='estadistica'
    )
]

forms.py     从Django导入表格     AnimalForm(forms.Form)类:

    animal = forms.CharField(
        label='Animal', 
        max_length=100
    )
    ciudad = forms.CharField(
        label='Ciudad',
        max_length=100
    )
    feria = forms.CharField(
        label='Feria',
        max_length=100
    )

0 个答案:

没有答案