如何使用Elasticsearch在Django中从前端编辑/删除数据?

时间:2018-12-03 09:58:05

标签: python django elasticsearch django-forms edit

我想从后端(postgresql)到前端检索数据,以便我可以编辑/删除它。

注意:我的项目正在使用django_elasticsearch_dsl_drf

我已经在views.py中实现了这一点:

def update(request, id):
    resp = Response.objects.get(pk = id)
    resp.response = request.POST.get('Response')
    resp.save()
    return render(request, 'app/update', {
    })

urls.py:

url(r'update/(\d+)/$', views.update, name="update")

我如何在表单中显示它?

update.html:

<table class="dataTable">

{% for field in qform.visible_fields %}
    <tr>
        <th>{{ field.label_tag }}*</th>
        <td>{{ field }}</td>
    </tr>
{% endfor %}
    <tr>
        <th>{{ rform.Response.label_tag }}*</th>
        <td>{{ rform.Response }}</td>
    </tr>
    <tr>
        <th>{{ rform.Topic.label_tag }}*</th>
        <td>{{ rform.Topic }}</td>
    </tr>
    <tr>
        <th>{{ rform.Client.label_tag }}*</th>
        <td>{{ rform.Client }}</td>
    </tr>
    <tr>
        <th>{{ rform.Planit_location.label_tag }}*</th>
        <td>{{ rform.Planit_location }}</td>
    </tr>
    <tr>
        <th>{{ rform.Date_added.label_tag }}</th>
        <td>{{ rform.Date_added }}</td>
    </tr>
    <tr>
        <th>{{ rform.Document.label_tag }}</th>
        <td>{{ rform.Document }}</td>
    </tr>

当我尝试访问url ... / update时,我也收到断言错误“未提供异常消息”

编辑 StackTrace:

Internal Server Error: /update/3/
django_1         | Traceback (most recent call last):
django_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
django_1         |     response = get_response(request)
django_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
django_1         |     response = self.process_exception_by_middleware(e, request)
django_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
django_1         |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_1         |   File "/app/app/views.py", line 358, in update
django_1         |     "rform": resp,
django_1         |   File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
django_1         |     content = loader.render_to_string(template_name, context, request, using=using)
django_1         |   File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 61, in render_to_string
django_1         |     template = get_template(template_name, using=using)
django_1         |   File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 19, in get_template
django_1         |     raise TemplateDoesNotExist(template_name, chain=chain)
django_1         | django.template.exceptions.TemplateDoesNotExist: app/update/

1 个答案:

答案 0 :(得分:1)

您的网址路径应为:

url(r'update/(?P<id>\d+)/$', views.update, name="update")

此外,您需要从更新方法中返回一些信息:

   return render(request, 'update_add.html', { 'rform': resp })