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')
我正试图将我的联系表格与数据库连接起来,但经过张贴方法后,它不允许我。
应该是get()
而不是get[]
,而且在处理POST数据后返回HttpResponseRedirect是一个很好的做法,这个技巧不是Django特有的,它通常是一个好的web实践,所以视图应该是:
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')