为什么用户输入不会进入数据库?
问题描述:
截至目前,我的数据库工作正常。我可以手动创建虚拟电子邮件地址'通过Terminal
,他们会出现没有问题。为什么用户输入不会进入数据库?
这次,我想让用户输入数据库。我觉得我与我现在的代码非常接近。不过,我不断收到这个error
在Chrome
:
这里的error
:
TypeError at /content/content/
'name' is an invalid keyword argument for this function
Request Method: POST
Request URL: http://127.0.0.1:8000/content/content/
Django Version: 1.11.3
Exception Type: TypeError
Exception Value:
'name' is an invalid keyword argument for this function
这里的basic.html
:
{% extends "personal/header.html" %}
{% block content %}
<style type="text/css">
h1 {
color: #2e6da4;
font-family: Chalkboard;
}
.text {
text-align: center;
}
</style>
{% for c in content %}
<h1>{{c}}</h1>
{% endfor %}
<div class="form-group">
<form method="POST" action="content/">
{% csrf_token %}
<input type="text" name="textfield">
<button type="submit">Submit</button>
</form>
</div>
{% endblock %}
这里的views.py
:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Email
from django.core.exceptions import *
def index(request):
return render(request, 'personal/home.html')
def contact(request):
if request.method == "GET":
return render(request, 'personal/basic.html', {'content': ['If you would like more information, leave your email.']})
elif request.method == "POST":
email = Email(name=request.POST.get("textfield"))
email.save()
return render(request, 'basic.html')
def search(request):
if request.method == 'POST':
search_id = request.POST.get(name = search_id)
try:
user = Email.objects.get(name=search_id)
# do something with user
html = ("<H1>%s</H1>", user)
return HttpResponse(html)
except Email.DoesNotExist:
return HttpResponse("no such user")
else:
return render(request, 'basic.html')
这里是urls.py
:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^content/', views.contact, name='content'),
]
这里的models.py
:
from django.db import models
class Email(models.Model):
email = models.CharField(max_length=140)
def __str__(self):
return self.email
答
你需要更换,因为在你的模型中没有提起name
,但有场email
email = Email(name=request.POST.get("textfield"))
到
email = Email(email=request.POST.get("textfield"))
# ^^^^^
你对此有何看法? 'request.POST.get(name = search_id)' – janos
就像错误说的那样,'name'不是该类的有效关键字。模型实际上是否有名称字段?你应该展示模型。 –
请显示你的'class Email' –