如何在django中显示表单上的内容

时间:2016-06-23 07:46:09

标签: html django django-forms

我尝试在Django中显示表单上的内容。我想在搜索栏输入电子邮件时显示结果以作出响应。如何将结果显示给响应?

views.py

from django.shortcuts import render
from django.utils import timezone
import requests
from .models import StorageList
from django.db.models import Q

def xGET(x_auth_user, x_auth_key):
    url = 'https://ssproxy.ucloudbiz.olleh.com/auth/v1.0'
    headers = {'X-Storage-User': x_auth_user, 'X-Storage-Pass': x_auth_key}
    response = requests.get(url, headers=headers)
    print response.headers

def storage_list(request):
    today = timezone.now().date()
    queryset_list = StorageList.objects.all()
    for data in queryset_list:
        MEM_ID = data.mem_id
        MEM_SQ = data.mem_sq
        X_AUTH_USER = data.x_auth_user
        X_AUTH_KEY = data.x_auth_key
        URL = data.x_storage_url
        API_KEY = data.accesskey
        query = request.GET.get('q')
        if query == MEM_ID:
            xGET(X_AUTH_USER, X_AUTH_KEY)

    return render(request, "storage/storage_list.html")

模板

{% extends "base.html" %}
{% block content %}

<div class='col-sm-6 col-sm-offset-3'>
    <h1>{{ title }}</h1>
    <form method='GET' action='' class='row'>
        <div class='col-sm-6'>
            <div class='input-group'>
                <input class='form-control' type='text' name='q' placeholder='Search posts' value='{{ request.GET.q }}'/>
                <span class='input-group-btn'>
                    <!-- <input class='btn btn-default' type='submit' value='Search' /> -->
                    <button class='btn btn-default' type='submit'>Search <i class="fa fa-search"></i></button>
               </span>
            </div>
        </div>
    </form>
{% endblock content %}

我的网页

enter image description here

响应

[23/Jun/2016 16:40:49] "GET /storage/ HTTP/1.1" 200 5844
{'Content-Length': '126', 'X-Trans-Id': 'tx76ab9e218a2c4e4c9ed5642c89ae1033', 'X-Auth-Token-Expires': '77233', 'X-Auth-Token': 'AUTH_tkb6e12312141494254', 'Connection': 'close', 'X-Storage-Token': 'AUTH_t1231321131413c9b94254', 'Date': 'Thu, 23 Jun 2016 07:40:52 GMT', 'X-Storage-Url': 'https://ssproxy.ucloudbiz.olleh.com/v1/AUTH_be2b4d4d-3e5d-487c-bf31-bc42f7cf9ce8', 'Content-Type': 'text/html; charset=UTF-8'}

1 个答案:

答案 0 :(得分:0)

我不知道为什么你不使用现有的django框架函数,如forms。但要将response发送给"storage/storage_list.html",您可以尝试:

def xGET(x_auth_user, x_auth_key):
    url = 'https://ssproxy.ucloudbiz.olleh.com/auth/v1.0'
    headers = {'X_Storage_User':x_auth_user, 'X_Storage_Pass':x_auth_key}
    response = requests.get(url, headers=headers)
    return response

def storage_list(request):
    today = timezone.now().date()
    queryset_list = StorageList.objects.all()
    for data in queryset_list:
        MEM_ID = data.mem_id
        MEM_SQ = data.mem_sq
        X_AUTH_USER = data.x_auth_user
        X_AUTH_KEY = data.x_auth_key
        URL = data.x_storage_url
        API_KEY = data.accesskey
        query = request.GET.get('q')
        if query == MEM_ID:
            response = xGET(X_AUTH_USER, X_AUTH_KEY)
            # if user matched send the response
            return render(request, "storage/storage_list.html",context = response)
    return render(request, "storage/storage_list.html")

<强>模板

{% extends "base.html" %}
{% block content %}

<div class='col-sm-6 col-sm-offset-3'>
    <h1>{{ title }}</h1>
    <form method='GET' action='' class='row'>
        <div class='col-sm-6'>
            <div class='input-group'>
                <input class='form-control' type='text' name='q' placeholder='Search posts' value='{{ request.GET.q }}'/>
                <span class='input-group-btn'>
                    <!-- <input class='btn btn-default' type='submit' value='Search' /> -->
                    <button class='btn btn-default' type='submit'>Search <i class="fa fa-search"></i></button>
               </span>
            </div>
        </div>
    </form>
{% if X_Storage_User and X_Storage_Pass %}
  User: {{X_Storage_User}}
  Pass: {{X_Storage_Pass}}
{% endif %}
{% endblock content %}