我已经创建了一个联系表单,字段为name, email, and dropdown
。提交表单时出现错误,但是如果我打印(名称),它将显示在终端上。
contactus()收到了意外的关键字参数“名称”
您能帮我吗?
contatus.html
<form action="/contactus/" method="post">
{% csrf_token %}
<div class="input-block">
<input id="name" name="name" type="text" placeholder="your full name" class="form-control">
</div>
<div class="input-block">
<select name="year" id="year" class="form-control">
<option selected disabled>no of Year</option>
<option value="1">1 Year</option>
<option value="2">2 Year</option>
</select>
</div>
<div id="reachEmail" class="input-block">
<input id="email" name="email" type="email" class="form-control" placeholder="email">
</div>
<input type="submit" name="Send" value="SEND">
</form>
view.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from .models import contactus
def home(request):
return render(request, 'demo1/home.html', {'': ''})
def contactus_submit(request):
if request.method == "POST":
name=request.POST.get('name','')
year=request.POST.get('year','')
email=request.POST.get('email','')
contact=contactus(name=name,year=year,email=email)
contact.save();
return render(request, 'demo1/contactus.html')
Model.py
class contactus(models.Model):
id=models.AutoField(primary_key=True)
name = models.CharField(max_length=30)
year = models.CharField(max_length=30)
email = models.CharField(max_length=30)
admin.py
from .models import contactus
admin.site.register(contactus)
答案 0 :(得分:2)
遵循django的命名约定。
模型
class Contact(models.Model):
id=models.AutoField(primary_key=True)
name = models.CharField(max_length=30)
year = models.CharField(max_length=30)
email = models.CharField(max_length=30)
观看次数
def contact_us(request):
if request.method=="POST":
name=request.POST.get('name','')
year=request.POST.get('year','')
email=request.POST.get('email','')
contact=Contact(name=name,year=year,email=email)
contact.save()
return render(request,'demo1/contactus.html')
答案 1 :(得分:1)
contact=contactus(name=name,year=year,email=email)
此语句试图调用您的视图。
将视图名称更改为contact_view
。