视图没有返回 - Django

时间:2015-02-24 12:15:24

标签: python django

当发现视图发送一个get时,它返回什么都没有 - 没有看到http响应。知道我在视图上出错了吗?

# view.py

from AWSReporting.PythonScripts import disco
from AWSReporting.forms import EC2DiscoveryForm



# Defining discovery view
def discovery(request):
    # Loading django template
    # if POST request is recieved
    if request.method == 'POST':
        # Obtains POST information from form. 
        form = EC2DiscoveryForm(request.POST)
        # Will then check if the form is valid.
        if form.is_valid():
            # if valid form recieved, process disco function with ReportName from form.
            disco.EC2Discovery(form['ReportName'])
            # Returns user to Index
            return HttpResponseRedirect('/index/')
    # Else return empty form to GET request
    else:
        form = EC2DiscoveryForm()
    #Renders page with empty form with defined template. 
    return render(request, 'AWSReporting/EC2Discovery.html', {'form': form})

网址正确并指向正确的网页     #urls.py

from django.conf.urls import patterns, url

from AWSReporting import views


urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^post$', views.post_page, name='postjson'),
    url(r'^EC2Discovery$', views.discovery, name='EC2Discovery')
)

这是视图执行的功能,手动测试它工作正常。     #dcco.EC2Discovery()

import requests
from AWSReporting.models import Discoveries, DiscoveriesMade

def EC2Discovery(reportNameString):

    # Creating discovery index and commiting to database.
    newReport = Discoveries(reportName=reportNameString, status='Running')
    newReport.save()

    # Pulling back commited object to obtain the Primary Key and storing as an int.
    reportVar = Discoveries.objects.get(reportName=reportNameString).id
    reportInt = int(reportVar)

    # Formatting url string to PoSHServer with primary key.
    urlstring = "http://172.16.116.130:8080/Discovery.ps1?command={}" .format(reportInt)

    # sends GET to urlstring.
    r = requests.get(urlstring, timeout=5)
    # encoding unicode respone to string storing in var.
    var = r.text.encode('utf-8')

    # PoSHServer doesn't always respond, use while loop to ensure request has been successful
    while True:
        # If Success string is not seen, try again.
        if '\xc3\xaf\xc2\xbb\xc2\xbfSuccsess\r\n' not in var:
            # Sending request to urlstring.
            r = requests.get(urlstring, timeout=5)
            # Encoding unicode respone to string storing in var for if check.
            var = r.text.encode('utf-8')
        # Breaks while when successful
        if '\xc3\xaf\xc2\xbb\xc2\xbfSuccsess\r\n' in var:
            break

当向发现视图发送GET时,会返回一个异常,表明没有看到HTTP响应,视图没有返回任何内容。

看到异常

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/EC2Discovery

Django Version: 1.7.4
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'AWSReporting')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/handlers/base.py" in get_response
  130.                                  % (callback.__module__, view_name))

Exception Type: ValueError at /EC2Discovery
Exception Value: The view AWSReporting.views.discovery didn't return an HttpResponse object. It returned None instead.

0 个答案:

没有答案