Django的表单实例出错

时间:2017-08-17 13:22:14

标签: python django forms

项目中的文件是:

djangoTry

- views.py

- forms.py

- 其他未包含在此问题文件

当我在表单中单击“提交”时,我将从views.py中调用此方法:

from .forms import NameForm
def kontakt(request):
if request.method == 'POST':
    form = NameForm(request.POST)
    form.test("test")
    if form.is_valid():
        first_name = form.cleaned_data['first_name']
        last_name = form.cleaned_data['last_name']
        phone_number = form.cleaned_data['phone_number']
        email = form.cleaned_data['email']
        details = form.cleaned_data['details']
        return HttpResponseRedirect('/')

else:
    form = NameForm()

return render(request, 'index.html', {'form':form})

NameForm是forms.py文件中的类:

from django import forms

class NameForm(forms.Form):
    first_name = forms.CharField(label='first_name', max_length=100)
    last_name = forms.CharField(label='last_name', max_length=100)
    phone_number = forms.CharField(label='phone_number', max_length=100)
    email = forms.CharField(label='email', max_length=100)
    details = forms.CharField(label='details', max_length=100)
    def test(self, message):
        print("i'm in test and message is: %s " , (message))
        print(self.first_name)

    def is_valid(self):
        print("jest valid")
        return True

form.html

<form class="col s12" action="{% url 'kontakt' %}" method="post">

        {% csrf_token %}
        {{ form }}
        <div class="row">
          <div class="input-field col s6">
            <input 
              id="first_name" 
              type="text" 
              value="hehe">
              <!-- value="{{ form.first_name }}"> -->
            <label for="first_name">First name</label>
          </div>
          <div class="input-field col s6">
            <input 
              id="last_name" 
              type="text" 
              autocomplete="off" 
              value="hehe">

              <!-- value="{{ form.last_name }}" > -->
            <label for="last_name">Last name</label>
          </div>
        </div>
        <div class="row">
          <div class="input-field col s12">
            <input id="phone_number" type="number" autocomplete="off" 
              value="123456789">
            <label for="phone_number">Phone number</label>
          </div>
        </div>
        <div class="row">
          <div class="input-field col s12">
            <input id="email" type="email" autocomplete="off" value="rafald121@gmail.com" >
            <label for="email">Email</label>
          </div>
        </div>
        <div class="row">
          <div class="input-field col s12">
            <input id="details" type="text" autocomplete="off" value="qweqweqeq">
            <label for="details">Details</label>
          </div>
        </div>
        <div class="row">
          <div class="input-field col s12">
            <a class="waves-effect waves-light btn">
              <input id="submit" type="submit" >
              <i class="material-icons right">send</i>
            </a>
            <!-- <input id="submit" type="submit" > -->
            <label for="details">Details</label>
          </div>
        </div>        
      </form>

但每次我都会收到错误:

AttributeError: 'NameForm' object has no attribute 'first_name'

但NameForm有&#34; first_name&#34;属性附加伤害

NameForm&#39;方法&#34;测试&#34;每次都能正常工作,但任何NameForm的变量都无法调用。

有人知道发生了什么吗?

0 个答案:

没有答案