SearchMorm未显示在category.html中

时间:2018-01-12 06:36:29

标签: python django

search.html中未显示SearchForm。 我在views.py中写道

;WITH cte AS
(
SELECT empid
    , MIN(timein) timein
    , MAX(timeout) timeout
    , cast(coalesce(timein, timeout) AS DATE) d
    FROM @T
    GROUP BY empid, cast(coalesce(timein, timeout) AS DATE)
)
SELECT empid
, d AS Day
, DateDiff(HOUR, TimeIn, TimeOut) [Hours Worked]
FROM cte
ORDER BY d ASC
category.html中的

def top(request):
    content = POST.objects.order_by('-created_at')[:5]
    form = SearchForm()

    return render(request,'top.html',{'content':content,'form':form})

class DetailView(generic.DetailView):
    model = Detail
    template_name = 'detail.html'
    form = SearchForm()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

我认为如果我编写了类似于<div class="search_box"> <form action='/app/search/' method="POST"> <table> {{ form.as_table }} </table> <button class="btn btn-secondary search" type="submit">SEARCH</button> {% csrf_token %} </form> </div> div> <h2>{{ object.title }}</h2> <a href="{% url 'detail' content.pk %}">SHOW DETAIL</a> </div> 的top方法的DetailView类,我可以在detail.html中显示SearchForm()。但是,DetailView类是通用视图,所以我无法理解我如何阅读SearchForm()在DetailView类中。我写了context ['search_form'] = SearchForm()但是没有显示表单。我该如何解决这个问题?我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

class DetailView(generic.DetailView):
    model = Detail
    template_name = 'detail.html'
    form_class = SearchForm

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = self.form_class
        return context