截至目前,我的數據庫工作正常。我可以手動創建虛擬電子郵件地址'通過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
你對此有何看法? 'request.POST.get(name = search_id)' – janos
就像錯誤說的那樣,'name'不是該類的有效關鍵字。模型實際上是否有名稱字段?你應該展示模型。 –
請顯示你的'class Email' –