Django视图在模型更改后未更新

时间:2017-03-13 10:52:57

标签: python django

我遇到了正在处理的Django项目的问题。

我有一个视图,我通过POST请求创建一个模型实例:

class CreatePollView(View):
    template = "polls/create_poll.html"

    @method_decorator(login_required)
    def post(self, request):
        question_text = request.POST['question']
        pub_date = now()
        new_question = Question(question_text=question_text, pub_date=pub_date)
        new_question.save()

        options_list = request.POST.getlist('options')
        for option in options_list:
            option_text = option
            new_option = Option(option_text=option_text, question=new_question)
            new_option.save()

        return HttpResponseRedirect(reverse('polls:detail', args=(new_question.id,)))

这很好用。我可以将问题及其选项添加到数据库中,并看到它已添加到管理员中。但是当我然后调用我的index.html时,我列出了所有问题对象,它没有更新。

class IndexView(View):
    template = loader.get_template('polls/index.html')
    questions = Question.objects.all()
    context = {
        'question_list': questions,
    }

    def get(self, request):
        return HttpResponse(self.template.render(self.context, request))

模板:

{% extends 'homepage/base.html' %}
{% block content %}
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h1>All Polls:</h1>
            <div>
                {% for question in question_list %}
                    <p><a href="{{ question.pk }}">{{ question }}</a></p>

                {% endfor %}
                <a href="new_poll" class="btn btn-primary">New Poll</a>
            </div>
        </div>
    </div>
{% endblock %}

当我在视图中构建错误时,然后修复它并再次运行代码它会更新列表。 Question.objects.all()

但除此之外,当我发布新模型实例时,模型更改不会显示在此处。有人能告诉我我做错了吗?

编辑:这似乎只发生在基于类的视图上。当我使用方法视图时,它工作正常。

def index(request):
    question_list = Question.objects.all()
    template = loader.get_template('polls/index.html')
    context = {
        'question_list': question_list,
    }
    return HttpResponse(template.render(context, request))


def new_poll(request):
    if request.method == 'POST':
        question_text = request.POST['question']
        pub_date = now()
        new_question = Question(question_text=question_text, pub_date=pub_date)
        new_question.save()

        options_list = request.POST.getlist('options')
        # import pdb
        # pdb.set_trace()
        for option in options_list:
            option_text = option
            new_option = Option(option_text=option_text, question=new_question)
            new_option.save()

        return HttpResponseRedirect(reverse('polls:detail', args=(new_question.id,)))
    else:
        template = loader.get_template('polls/create_poll.html')
        context = {
            'user': request.user
        }
        return HttpResponse(template.render(context, request))

1 个答案:

答案 0 :(得分:3)

Django旨在作为一个长时间运行的进程(加载一次,永远服务),而不是作为一个CGI脚本,在每个HTTP请求上重新加载所有内容。这意味着在模块的第一次导入(井)中,在函数外部发生的任何事情(即在模块的顶级,在类声明的主体中等)都只执行一次(每个进程)就像任何Python进程一样 - 这里的重点是它是一个长时间运行的进程,而不是一次性脚本。)

所以这里:

class IndexView(View):
    template = loader.get_template('polls/index.html')
    questions = Question.objects.all()
    context = {
        'question_list': questions,
    }

您班级正文中的这三个陈述是在您的模块首次导入时使用class语句进行评估的。从那时起,这些值不会在流程生命周期内被重新规避,因此您确实会将context['question_list']的结果与请求保持一致,直到您终止服务器进程并启动一个新进程(这将在第一次请求之后变得陈旧等等。)

您不会遇到基于函数的视图的问题,因为您的代码会在每个请求上执行,从而产生最新的查询集。

简而言之,首先将此代码移至您的班级get方法:

class IndexView(View):
    def get(self, request):
        template = loader.get_template('polls/index.html')
        questions = Question.objects.all()
        context = {
            'question_list': questions,
        }
        return HttpResponse(template.render(context, request))

那么您可能需要花一点时间来学习django的formsmodelformsshortcuts等,如果您坚持使用基于类的视图,那么学会使用various mixins

相关问题