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}}" >
<div >
<div >
<form method="POST" action="/session/{{visitor_id}}">
{% csrf_token %}
<div >
<h4 name="taken_table">Masa {{i.table_number}}</h4>
<button
type="button"
data-bs-dismiss="modal"
aria-hidden="Close"
></button>
</div>
<div >
<div >
<label>İsim</label>
<input
name="taken_name"
type="text"
required
/>
</div>
</div>
<div >
<button
type="button"
data-bs-dismiss="modal"
aria-label="Close"
>
İptal
</button>
<a href="/session/{{visitor_no}}" type="submit" >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
})
CodePudding user response:
In your template, you are using the following:
<a href="/session/{{visitor_no}}" type="submit" >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" >Kaydet</button>