Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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中的视图文件获取类调用_Python_Django - Fatal编程技术网

函数未从python中的视图文件获取类调用

函数未从python中的视图文件获取类调用,python,django,Python,Django,当我从views.py调用该类时,它不会调用该类的函数。我正在调用函数latest\u question\u list=question.get\u poll\u question from views.py,但它不会打印在我的模型函数中 这是我的密码: views.py from django.shortcuts import render # Create your views here. from django.http import HttpResponse, HttpRespon

当我从views.py调用该类时,它不会调用该类的函数。我正在调用函数latest\u question\u list=question.get\u poll\u question from views.py,但它不会打印在我的模型函数中

这是我的密码:

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}
    return render(request, 'polls/index.html', context)

def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'polls/detail.html', {'question': question})

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):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': question,
            '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=(question.id,)))
models.py

import datetime
from django.utils import timezone
from django.db import connection

global cursor
cursor = connection.cursor()

class Question():

    print(123)

    def get_poll_question():
        print(456)
        db_table = "polls_question"
        cursor.execute('SELECT * FROM '+db_table)
        return allquestions

class Choice():


    def __str__(self):
        db_table = "polls_choice"
        cursor.execute("SELECT * FROM "+ db_table+" WHERE question_id = '1' ")
        choice_text = cursor.fetchall();
        return self.choice_text

您需要将该方法标记为classmethod。也可以使用全局游标 IMO是一个非常糟糕的主意

课堂问题: @类方法 def get_poll_questioncls: 使用connection.cursor作为游标: db_table=民意测验_问题 cursor.executefSELECT*来自{db_table} 返回cursor.fetchall 另一个最好的方法可能是创建一个模型。如果你不
控制表格,您可以在get_poll_question中创建一个

,返回所有问题,但您从未在任何地方定义此变量。为什么要编写自己的问题类和原始SQL查询?如果您正在学习Django,请使用Django模型,例如类Questionmodels。模型:。我的问题是为什么我没有打印:456,为什么它没有调用该函数?我尝试了您的代码,但它仍然没有在我的cli控制台中打印456