Django-如何将值从现有的HTML表单传递到列表视图

时间:2018-09-01 04:17:38

标签: django

我有一个现有的HTML表单(文本字段+按钮),但不知道如何将输入值传递到列表视图。

更新

book / home.html:

<form class="form-inline my-2 my-lg-0" method="get" action="{% url 'book:search' %}">{% csrf_token %}
    <input style="font-size: 12px; width: 200px" class="form-control mr-sm-2" name="search" type="search" placeholder="Book Name" aria-label="Search">
    <button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
</form>

图书/型号:

class Book(models.Model):

    title = models.CharField(max_length=191, unique=True)

    slug = models.SlugField(unique=True, null=True, allow_unicode=True)

    pub_date = models.DateField()

    ............

书/视图:

class SearchResultView(generic.ListView):
    template_name = 'book/search.html'
    model = Book
    paginate_by = 10

    def get_queryset(self):
        queryset = super().get_queryset()
        search = self.request.GET.get('search')
        if search:
            queryset.filter(title__icontains=search)
        return queryset


class BookDetailView(generic.DetailView):
    template_name = 'book/detail.html'
    model = Book

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['all_categories'] = Category.objects.all()
        return context

图书/网址:

app_name = 'book'
urlpatterns = [
    path('', views.HomePageView.as_view(), name='home'),
    path('<slug:slug>/', views.BookDetailView.as_view(), name='detail'),
    path('search/', views.SearchResultView.as_view(), name='search')
]

book / templates / search.html(仅用于测试):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1> here we go </h1>
</body>
</html>

浏览器错误: enter image description here

2 个答案:

答案 0 :(得分:1)

首先,对于搜索功能,您可以使用GET请求而不是POST。要将表单数据发送到特定视图,请使用action属性。另外,您还需要为name元素添加input属性:

<form class="form-inline my-2 my-lg-0" method="get" action="{% url 'view_urlname' %}">
    <input style="font-size: 12px; width: 200px" class="form-control mr-sm-2" type="search" placeholder="Book Name" name="search" aria-label="Search">
    <button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
</form>

要在视图中获取表单数据,请使用self.request.GET

class SearchResultView(generic.ListView):
    template_name = 'book/search.html'
    model = Book
    paginate_by = 100

    def get_queryset(self):
        queryset = super().get_queryset()
        search = self.request.GET.get('search')
        if search:
            queryset.filter(filedname_contains=search)
        return queryset

答案 1 :(得分:0)

如果您尝试创建自定义查询集并仅显示过滤器查询集而不显示完整查询集,则可以覆盖get_queryset()函数并通过访问self.request.POST获取表单输入。

def get_queryset(self):
    if self.request.method == 'POST':
        book_name = self.request.POST.get('book_name', '')
        return Book.objects.filter(name=book_name)  # i'm assuming that in your Book model the name field if called 'name'

然后,您只需要确保输入具有这样的名称即可:

<input name="book_name" style="font-size: 12px; width: 200px" class="form-control mr-sm-2" type="search" placeholder="Book Name" aria-label="Search">