지금은 내 데이터베이스가 제대로 작동합니다. 를 통해 수동으로 더미 이메일 주소를 생성 할 수 있으며 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"))
# ^^^^^
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다