Issue
Views.py
# Create your views here.
def home(request):
return render(request, 'home.html')
def about(request):
return render(request, 'about.html')
def project(request):
return render(request, 'project.html')
def contact(request):
if request.method=='POST':
name=request.POST.get['name']
email=request.POST.get['email']
phone=request.POST.get['phone']
concern=request.POST.get['concern']
print(name,email,phone,'concern')
obj=Contact(name='name', email='email',phone='phone',concern='concern')
obj.save()
return render(request, 'contact.html')
I am trying to connect my contact form with database but after post method it doesn't allow me.
Solution
It should be get()
not get[]
and also it's a good practice to return HttpResponseRedirect after dealing with POST data, the tip is not specific to Django, it's a good web practice in general, so the view should be:
def contact(request):
if request.method=='POST':
name=request.POST.get('name')
email=request.POST.get('email')
phone=request.POST.get('phone')
concern=request.POST.get('concern')
print(name,email,phone,concern)
obj=Contact(name=name, email=email,phone=phone,concern=concern)
obj.save()
return redirect('some_path_name')
return render(request, 'contact.html')
Answered By - Sunderam Dubey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.