django - 从 HTML

发布于 2024-10-14 15:37:34 字数 330 浏览 1 评论 0原文

我看过一些关于如何从 Django 中的 HTML 语句收集数据的文档,但对我来说都不是很清楚。有人有一个真实的工作示例可以分享吗?

就我而言,我的模板文件中有类似这样的内容:

<select title="my_options">
   <option value="1">Select value 1</option>
   <option value="2">Select value 2</option>
</select>

views.py 中为了收集选定的值会做什么?谢谢你!

I've seen a couple of documents on how to collect the data from a HTML statement in Django but none of them were very clear to me. Does anybody have a real working example to share?

In my case I have something like this in my template file:

<select title="my_options">
   <option value="1">Select value 1</option>
   <option value="2">Select value 2</option>
</select>

What goes in the views.py in order to collect the selected value? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城追梦 2024-10-21 15:37:34

如果是 GET 请求,则为 request.GET['my_options']。如果是 POST,则为 request.POST['my_options']。这将是一个字符串,"1""2" (或 "

无论哪种方式,最好使用 Django 表单框架 可以省去编写 HTML 和清理返回值的麻烦。

If it's a GET request, request.GET['my_options']. If it's a POST, then request.POST['my_options']. This will be a string, either "1" or "2" (or "<script>alert('I hacked you!')</script>")

Either way, it's probably better to use the Django forms framework to save you the hassle of writing the HTML, and sanitizing the returned values.

时光与爱终年不遇 2024-10-21 15:37:34

通过 POST 管理数据

def yourView(request):
    # Use '.get('id', None)' in case you don't receive it, avoid getting error
    selected_option = request.POST.get('my_options', None)  

    if selected_option:
        # Do what you need with the variable

Django 中的表单可能有用的一件事是,如果您对 URL 进行 POST 或只是加载它,则可以做不同的事情:

def yourView(request):

    if request.POST:  # If this is true, the view received POST
        selected_option = request.POST.get('my_options', None)
        if selected_option:
            # Do what you need to do with the variables
        return render_to_response(...)

return render_to_response(...)

有 2 个 render_to_response 以防您需要做不同的事情如果视图刚刚加载或收到 POST 的情况。

通过 GET 管理数据

def yourView(request):
    # Use '.get('id', None)' in case you don't receive it, avoid getting error
    selected_option = request.GET.get('my_options', None)  

    if selected_option:
        # Do what you need with the variable

Manage data via POST

def yourView(request):
    # Use '.get('id', None)' in case you don't receive it, avoid getting error
    selected_option = request.POST.get('my_options', None)  

    if selected_option:
        # Do what you need with the variable

One thing that may be useful with forms in Django is to make different things if you make a POST to the URL or just load it:

def yourView(request):

    if request.POST:  # If this is true, the view received POST
        selected_option = request.POST.get('my_options', None)
        if selected_option:
            # Do what you need to do with the variables
        return render_to_response(...)

return render_to_response(...)

There are 2 render_to_response in case you need to do different things if the view was just loaded or receive a POST.

Manage data via GET

def yourView(request):
    # Use '.get('id', None)' in case you don't receive it, avoid getting error
    selected_option = request.GET.get('my_options', None)  

    if selected_option:
        # Do what you need with the variable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文