많은 사람들이 이미이 오류가 발생했음을 알고 있습니다. 그러나 내가하려는 것은 구체적이며 그 다른 질문에 대한 답변은 도움이되지 않습니다. Django를 처음 사용하며 이름, 성, 이메일 및 비밀번호 필드가있는 양식을 만들려고합니다. 양식을 제출 한 후에 모든 파일을 파일로 작성하고 싶습니다. 저는 User 객체가 없으며 실제로 로그인 페이지를 만들려하지 않고, txt 파일에 어떻게 쓸 수 있는지보고 싶습니다.Django AttributeError : 폼 객체에는 속성이 없습니다. x
그래서, 난 urls.py에, 이런 짓을 :
views.py에서 다음url(r'^file/$', views.employee, name='employee')
:
class EmployeeForm(forms.Form):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
email = forms.EmailField()
password = forms.CharField(widget=forms.PasswordInput())
:
def employee(request):
if request.method == "POST":
form = EmployeeForm(request.POST)
if form.is_valid():
with open('employee.txt', 'w') as f:
myfile = File(f)
myfile.write('%s ... %s ... %s ... %s ' % form.first_name % form.last_name % form.email % form.password)
return render(request, 'blog/employee_thanks.html')
else:
form = EmployeeForm
return render(request, 'blog/employee.html', {'form': form})
은 그럼 내가 forms.py에 EmployeeForm이
및 해당 html 파일은 다음과 같습니다. employee.html :
{% extends 'blog/base.html' %}
{% block content %}
<h1>Employee Sign-in</h1>
<h3>Please sign in with your Credentials</h3>
<form method="post" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-primary">Sign in</button>
</form>
{% endblock %}
및 employee_thanks.html :
{% extends 'blog/base.html' %}
{% block content %}
<div class="container">
<h1>Thank you for signing in!</h1>
<h3>You will get an email with questions soon.</h3>
</div>
{% endblock %}
그리고 마지막으로, 문제의 오류 :
AttributeError at /file/ 'EmployeeForm' object has no attribute 'first_name' Request Method: POST Request URL: http://localhost:8000/file/ Django Version: 1.10 Exception Type: AttributeError Exception Value:
'EmployeeForm' object has no attribute 'first_name'
나는이 문제를 추측하고있어 forms.py 및 views.py 사이 어딘가에이지만, 나는 그것이 무엇인지 모릅니다. 나는 취미로서 이것을하고 있기 때문에, 질문이 멍청하다면 너무 가혹하지 마십시오. 미리 감사드립니다 :)
감사합니다, 이것은 매우 통찰력이 있었다. 나는 두 번째 입력이 .txt 파일의 첫 번째 입력을 덮어 쓰는 것처럼 보입니다. 내가 줄에서 "w"와 다른 것을 사용하기로되어 있나? open ('employee.txt', 'w') f로? 업데이트 : "추가"로 "a"를 사용해야합니다. 다른 사람이 필요로하는 경우를 대비해서 이것을 남겨두고 감사를 표하십시오. –
@MiodragNisevic : 맞습니다.''w ''가 파일을 덮어 쓰기 때문에''a ''를 사용하십시오. 각 양식 제출의 필드가 한 행에 표시되도록 각 쓰기 끝에 새 행을 추가 할 수 있습니다. – mhawke