简体   繁体   English

无法从 Django 中的 html 页面获取值

[英]Failed to get value from html page in Django

I have a problem with trying to get a response from my HTML page using Django (admin).我在尝试使用 Django(管理员)从我的 HTML 页面获取响应时遇到问题。 I have a pretty simple div = contenteditable and need to pass data from this div back after the submit button was clicked.我有一个非常简单的div = contenteditable并且需要在单击提交按钮后从该 div 传回数据。

Everything, including choosing selection and opening the intermediate page works fine.一切,包括选择选择和打开中间页面都可以正常工作。 But when I tapped submit button, the condition if "apply" in request.POST failed to work.但是当我点击提交按钮时, if "apply" in request.POST无法工作。

Please, tell me, what I'm doing wrong?请告诉我,我做错了什么?

This is my Django admin:这是我的 Django 管理员:

class QuestionAdmin(AnnotatesDisplayAdminMixin, admin.ModelAdmin):

def matched_skills(self, question):
    return ', '.join(s.name for s in question.skills.all())

def update_skills(self, request, queryset):
    if 'apply' in request.POST:
        print("something")
        
    skills = []
    for question in queryset:
        skills.append(self.matched_skills(question))
    return render(request,
                  'admin/order_intermediate.html',
                  context={'skills': skills})

update_skills.short_description = "Update skills"

This is my order_intermediate.html page:这是我的 order_intermediate.html 页面:

{% extends "admin/base_site.html" %}
{% block content %}
<form method="post">
    {% csrf_token %}

  <h1>Adjust skills. </h1>
  {% for skill in skills %}
    <div>
    <div id="title" style="margin-left: 5px" contenteditable="true" > {{ skill }} </div>
    </div>
  {% endfor %}

  <input type="hidden" name="action" value="update_status" />
  <input type="submit" name="apply" value="Update skills"/>
</form>
{% endblock %}

Actually, request.POST is an HttpRequest object. For getting available keys in the body of the request, you need to use "request.POST.keys()" method.实际上,request.POST 是一个 HttpRequest object。要获取请求正文中的可用密钥,您需要使用“request.POST.keys()”方法。 So, you can simply change your condition to:所以,你可以简单地将你的条件更改为:

if 'apply' in request.POST.keys():
    print("something")

In my knowledge, you can not send div content with form submit.据我所知,您不能通过提交表单发送 div 内容。 However you can use input tag with array in name attribute for this.但是,您可以为此在名称属性中使用带有数组的输入标签。 This will send an array as post variable when submit这将在提交时发送一个数组作为 post 变量

First, send skills as a enumerate object from your views首先,从您的观点中将技能作为枚举 object 发送

return render(request, 'admin/order_intermediate.html', context={'skills': enumerate(skills)})

Then edit your html to this (Note: if you have css in title id, change it to title class)然后将您的 html 编辑为此(注意:如果您的标题 ID 中有 css,请将其更改为标题类)

{% for i,skill in skills %}
    <div>
        <input class="title" name="skill[{{ i }}]" value="{{ skill }}" style="margin-left: 5px">
    </div>
{% endfor %}

and handle array with any action you want to perform in update_skills()并使用您想在 update_skills() 中执行的任何操作来处理数组

for skill in request.POST.getlist('skill[]'):
    # your code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM