I am an absolute beginner in Django development & I am unable to send the data to my database via the POST method. Please guide me on what is wrong with my approach. My model worked perfectly and I can now access my desired table on my Django admin. The function that I have created in views.py always executes the else condition.
From views.py:
from django.shortcuts import render, HttpResponse, redirect
from app.models import Tbl_Feedback
def myform(request):
return render(request, 'form.html')
def getfeedback(request):
if request == "POST":
a = request.POST.get('a')
objTbl_Feedback = Tbl_Feedback(a="a")
objTbl_Feedback.save()
return redirect("/")
else:
return HttpResponse('Form Not Submitted')
From models.py:
from django.db import models
# Create your models here.
class Tbl_Feedback(models.Model):
fdbk = models.CharField(max_length=120)
From urls.py(app):
from django.contrib import admin
from django.urls import path
from app import views
urlpatterns = [
path('',views.myform,name="form"),
path('getfeedback', views.getfeedback, name="feedback")
]
From urls.py(project):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("app.urls"))
]
Html:
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form</title>
{% load static %}
<link rel="stylesheet" href="{%static 'css.css'%}">
</head>
<body>
<form action="getfeedback" method="post" >
{% csrf_token %}
<div class="frame">
<div class="frame-header">FeedBack Please !</div>
<div class="frame-body">
<div class="form-element">
<div class="element-label"><label for="a">FeedBack</label></div>
<div class="element-controller">
<textarea name="a" id="a" cols="30" rows="5" class="controller-input"
autofocus="autofocus" maxlength="120"></textarea>
</div>
</div>
</div>
<div class="frame-footer"><button type="submit">Submit</button> </div>
</div>
</form>
</body>
</html>
CodePudding user response:
Django templates will not understand view names, when the form is submitted the values will get returned to the same view from which the request is sent.
In your views.py
from django.shortcuts import render, HttpResponse, redirect
from app.models import Tbl_Feedback
def myform(request):
if request.method == "GET":
return render(request, 'form.html')
if request == "POST":
a = request.POST.get('a')
objTbl_Feedback = Tbl_Feedback(a="a")
objTbl_Feedback.save()
return redirect("/")
and your app urls.py should be like this
from django.contrib import admin
from django.urls import path
from app import views
urlpatterns = [
path('',views.myform,name="form"),
]
and in your template remove form action
<form method="post">
CodePudding user response:
To know how to use and submit form in Django : here.
In your case (and generally), this how you can do to make a simple POST request on Django.
views.py ( It is a common solution to handle both POST and GET request by the same view. I'll do it this way )
def feedback_form(request): # Handle POST requests if request.method == 'POST': # It's request.method == 'POST', not request == 'POST' a = request.POST.get('a') objTbl_Feedback = Tbl_Feedback(a="a") objTbl_Feedback.save() # Take the habit to use url's name instead of hard coded url return redirect("form") else: # Handle non POST requests return render(request, 'form.html')
urls.py
urlpatterns = [ path('',views.feedback_form,name="form"), # This view handle POST and GET request # path('getfeedback', views.getfeedback, name="feedback") ]
urls.py (project) Don't change
form.html
Here you have to adjust the action
attribute of form
<!-- Again use Django url tag instead of hard coded url -->
<form action="{% url 'form' %}" method="post" >
...
</form>