2

I want to take values from a POST request. I want to take "taken_name" value from html form to views.

I tried several question's answers, but I think I' m missing very simple thing. I can not figure it out nearly 2 hours.

Parameter name in html is 'taken_name'.

Also urls and views parameters are true and matching with the methods which mentioned on stackoverflow.

I' m getting this error :

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

This means I can't get the value from html.

Tried: 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
    })
6
  • you can try request.data["key"] Commented May 19, 2022 at 11:27
  • i tried but didn't work. Commented May 19, 2022 at 11:30
  • 1
    I saw you html file I think you can try use submit button in your form section instead of using a tag as you already specify action in your form. try to replace a tag with submit button. Commented May 19, 2022 at 11:34
  • thank you very much ! you made my day sir. Commented May 19, 2022 at 11:38
  • Anytime. You can upvote comment :) Commented May 19, 2022 at 11:39

1 Answer 1

2

In your template, you are using the following:

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

If you were intending to use this to submit your form, it won't work. It will simply link to the URL nominated in the href attribute.

Try something like:

<button type="submit" class="btn btn-danger">Kaydet</button>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. it was like 2 hours horror movie for me. you made my day sir !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.