2017-08-11 147 views
-1

截至目前,我的數據庫工作正常。我可以手動創建虛擬電子郵件地址'通過Terminal,他們會出現沒有問題。爲什麼用戶輸入不會進入數據庫?

這次,我想讓用戶輸入數據庫。我覺得我與我現在的代碼非常接近。不過,我不斷收到這個errorChrome

這裏的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 
+1

你對此有何看法? 'request.POST.get(name = search_id)' – janos

+1

就像錯誤說的那樣,'name'不是該類的有效關鍵字。模型實際上是否有名稱字段?你應該展示模型。 –

+0

請顯示你的'class Email' –

回答

1

你需要更換,因爲在你的模型中沒有提起name,但有場email

email = Email(name=request.POST.get("textfield")) 

email = Email(email=request.POST.get("textfield")) 
#    ^^^^^ 
+0

感謝您的回覆。我嘗試過,但現在我得到一個錯誤,說'TemplateDoesNotExist at/content/content /'。 – hop38

+0

我想你需要將'return render(request,'basic.html')'替換爲'return render(request,'personal/basic.html')' –

+0

非常感謝!有效!現在我明白我犯的錯誤了。再次感謝人! – hop38

相關問題