sách gpt4 ai đã đi

django - Django 模板中的搜索字段

In lại 作者:行者123 更新时间:2023-12-05 08:13:51 32 4
mua khóa gpt4 Nike

Django 模板中的搜索字段

如何在类似于此图像的 Django 模板中创建搜索字段

http://asciicasts.com/system/photos/1204/original/E354I01.png

我在github上试试这个 https://github.com/rg3915/vendas/commit/e0c67fd8154a3b8e450ec3db38705cdd7efc1350

但我不知道如何完成。

1 Câu trả lời

您正在谈论搜索字段,但本质上只是一个表单,您有一个输入(搜索框)并且您收到该输入在您看来。

管理表单和 GET 操作的小例子:

views.py:

def your_view(request):
''' This could be your actual view or a new one '''
# Your code
if request.method == 'GET': # If the form is submitted

search_query = request.GET.get('search_box', None)
# Do whatever you need with the word the user looked for

# Your code

模板

在你的模板中,最重要的是表单,你应该有这样的东西:

# Your template code




# Your template code
  • action='.' <-- 这告诉 django 在与您相同的 URL 中执行 GET 操作
  • action='/other/url/' <-- 这告诉 django 在该 URL 中执行 GET
  • 此表单只是一个 HTML 表单,您也可以使用 Django Forms

urls.py

您的 URL 文件必须与之前的相同。您不需要对您的实际 URL 做任何更改,它应该是这样的:

url(r'^your_url/?$', 'yourproject.views.your_view', name='your_url_name'),

无论如何,我建议您查看一些信息,例如:

关于django - Django 模板中的搜索字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27112729/

32 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com