Django POST 请求参数 returns none in views.py

Django POST request parameters returns none in views.py

我想从 POST 请求中获取值。我想从 html 表单中获取“taken_name”值到视图。

我尝试了几个问题的答案,但我认为我遗漏了非常简单的事情。想了将近2个小时都想不通

html 中的参数名称是 'taken_name'。

另外,urls 和 views 参数都是正确的,并且与 Whosebug 上提到的方法匹配。

我收到此错误:

'null value in column "visitor_name" of relation "visitor" violates not-null constraint DETAIL: Failing row contains (15, null, null, null).'

这意味着我无法从 html 中获取值。

试过: get value from POST request in Django

res.html

{% for i in allList %}
<div id="createRecordModal-{{forloop.counter}}" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <form method="POST" action="/session/{{visitor_id}}">
        {% csrf_token %}
        <div class="modal-header">
          <h4 class="modal-title" name="taken_table">Masa {{i.table_number}}</h4>
          <button
            type="button"
            class="btn-close btn-close-white"
            data-bs-dismiss="modal"
            aria-hidden="Close"
          ></button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>İsim</label>
            <input
              name="taken_name"
              type="text"
              class="form-control"
              required
            />
          </div>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary"
            data-bs-dismiss="modal"
            aria-label="Close"
          >
            İptal
          </button>
          <a href="/session/{{visitor_no}}" type="submit" class="btn btn-danger">Kaydet</a>
        </div>
      </form>
    </div>
  </div>
</div>
{% endfor %}

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.loginUser, name="login"),
    path('reservation', views.reservation, name="reservation"),
    path('delete/<str:table_number>', views.delete, name='delete'),
    path('create/<str:table_number>', views.create, name='create'),
    path('session/<str:visitor_id>', views.createSession, name='create2')
]

views.py

def createSession(request, visitor_id):
    addSession = visitor.objects.filter(visitor_id = visitor_id)
    if request.method == "POST":
        taken_name = request.POST.get('taken_name')
        taken_surname = request.POST.get('taken_surname')
        taken_time = request.POST.get('taken_time')
        print(taken_name)
        print(taken_surname)
        print(taken_time)
    else:
        print('method is not POST')

    addSession.create(
        visitor_name = taken_name,
        visitor_surname = taken_surname,
        res_time = taken_time
    )
    context = {
    'addSession': addSession
    }
    return redirect('reservation')

def reservation(request):
    allList = table_inventory.objects.all()
    max_rating = table_inventory.objects.aggregate(Max('table_number')).get('table_number__max')
    visitor_no = visitor.objects.aggregate(Max('visitor_id')).get('visitor_id__max')
    visitor_no +=1
    return render(request, 'reservation.html', {
        'allList': allList,
        'max_rating': max_rating,
        'visitor_no': visitor_no
    })

在您的模板中,您使用了以下内容:

<a href="/session/{{visitor_no}}" type="submit" class="btn btn-danger">Kaydet</a>

如果您打算使用它来提交您的表单,它将无法工作。它将简单地link到href属性中指定的URL。

试试这样的东西:

<button type="submit" class="btn btn-danger">Kaydet</button>