Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django,测试应用程序的行为_Python_Django - Fatal编程技术网

Python Django,测试应用程序的行为

Python Django,测试应用程序的行为,python,django,Python,Django,将文件/Django apps/poolApp/polls/url.py定义如下: from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

将文件/Django apps/poolApp/polls/url.py定义如下:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
from django.http      import Http404
from django.shortcuts import render
from django.shortcuts import get_object_or_404, render
from django.http      import HttpResponse

from .models import Question

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'

class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)   

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question': question})

def vote(request, question_id):
    p = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': p,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
我正在尝试测试应用程序的行为:

>>> from django.test import Client
>>> client = Client()
>>> response = client.get('/')
现在我正在从那个地址中提取一个404,但是我得到了这个错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/test/client.py", line 500, in get
    **extra)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/test/client.py", line 303, in get
    return self.generic('GET', path, secure=secure, **r)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/test/client.py", line 379, in generic
    return self.request(**r)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/test/client.py", line 466, in request
    six.reraise(*exc_info)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/handlers/base.py", line 119, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py", line 366, in resolve
    for pattern in self.url_patterns:
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py", line 402, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/urlresolvers.py", line 396, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/devuser/Django-apps/poolApp/poolApp/urls.py", line 20, in <module>
    url(r'^polls/', include('polls.urls', namespace="polls")),
  File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/devuser/Django-apps/poolApp/polls/urls.py", line 3, in <module>
    from . import views
  File "/home/devuser/Django-apps/poolApp/polls/views.py", line 8, in <module>
    class IndexView(generic.ListView):
NameError: name 'generic' is not defined
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
get中的文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/test/client.py”,第500行
**额外费用)
get中的文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/test/client.py”,第303行
返回self.generic('GET',path,secure=secure,**r)
文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/test/client.py”,第379行,通用格式
返回自我请求(**r)
请求中的文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/test/client.py”,第466行
六、重放(*exc_信息)
文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/core/handlers/base.py”,第119行,在get_响应中
解析程序匹配=解析程序.resolve(请求.path\u信息)
文件“/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/Django/core/urlresolvers.py”,第366行,解析
对于self.url_模式中的模式:
文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/core/urlresolvers.py”,第402行,url_模式
patterns=getattr(self.urlconf_模块,“urlpatterns”,self.urlconf_模块)
urlconf_模块中的文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/core/urlresolvers.py”,第396行
self.\u urlconf\u module=import\u模块(self.urlconf\u名称)
文件“/usr/lib/python2.7/importlib/_init_uuu.py”,第37行,在导入模块中
__导入(名称)
文件“/home/devuser/Django apps/poolApp/poolApp/url.py”,第20行,在
url(r“^polls/”,包括('polls.url',namespace=“polls”),
include中第33行的文件“/usr/local/lib/python2.7/dist packages/Django-1.8.3-py2.7.egg/Django/conf/urls/__init___;.py”
urlconf_模块=导入_模块(urlconf_模块)
文件“/usr/lib/python2.7/importlib/_init_uuu.py”,第37行,在导入模块中
__导入(名称)
文件“/home/devuser/Django apps/poolApp/polls/url.py”,第3行,在
从…起导入视图
文件“/home/devuser/Django apps/poolApp/polls/views.py”,第8行,在
类索引视图(generic.ListView):
NameError:未定义名称“通用”

您必须在
views.py
中导入
django.views.generic
,如下所示:

from django.views import generic