AttributeError (Django) - PullRequest
       4

AttributeError (Django)

0 голосов
/ 25 марта 2020

Я получаю сообщение об ошибке:

'FeedBackForm' object has no attribute 'FeedBackForm'

Я перепробовал все шаги по устранению неполадок, но безуспешно. Будет очень заметно, если кто-нибудь сможет помочь.

form.py

from django import forms

class FeedBackForm(forms.Form):
    name=forms.CharField()
    Ldap_id=forms.CharField()
    email=forms.EmailField()
    company_name=forms.CharField()
    feedback=forms.CharField(widget=forms.Textarea)

views.py

from django.shortcuts import render
from testapp import forms

# Create your views here.

def feedback_view(request):
    form = forms.FeedBackForm()
    # As here we are sending details to register.html and in html file 
    # we have not mentioned action where this file will go
    # so it will come back here in views.py file only. :)
    if request.method == 'POST':
        form1 = form.FeedBackForm(request.POST)
        if form1.is_valid():
            print("Form Validation sucess and printing feedback info")
            # Now to capture data we will use cleaned_data===>cleaned_data==>{name:value}
            print('Name of editor:', form1.cleaned_data['name'])
            print('LDAP of editor:', form1.cleaned_data['Ldap_id'])
            print('EmailId:', form1.cleaned_data['email'])
            print('Company:', form1.cleaned_data['company'])
            print('Feedback provided:', form1.cleaned_data['feedback'])

        # Note this above if form.is_valid(): will start working 
        # only when we will submit form otherwise it will not start.

    my_dict = {'form': form}
    return render(request, 'testapp/register.html', context=my_dict)

urls.py

from django.contrib import admin
from django.urls import path
from testapp import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/',views.feedback_view),
]

1 Ответ

0 голосов
/ 25 марта 2020

Нельзя использовать form.FeedbackForm, поскольку вы присвоили form как FeedbackForm объект. Вы должны использовать forms.FeedbackForm:

from django.shortcuts import render
from testapp import forms

def feedback_view(request):
    form = forms.FeedBackForm()
    # As here we are sending details to register.html
    # and in html file we have not mentioned action where
    # this file will go so it will come back here in views.py
    # file only. :)
    if request.method == 'POST':
        form1 = <b>forms.FeedBackForm(request.POST)</b>
        if form1.is_valid():
            print("Form Validation sucess and printing feedback info")
            # Now to capture data we will use cleaned_data===>cleaned_data==>{name:value}
            print('Name of editor:', form1.cleaned_data['name'])
            print('LDAP of editor:', form1.cleaned_data['Ldap_id'])
            print('EmailId:', form1.cleaned_data['email'])
            print('Company:', form1.cleaned_data['company'])
            print('Feedback provided:', form1.cleaned_data['feedback'])

        # Note this above if form.is_valid(): will start working only 
        # when we'll submit form otherwise it will not start.

    my_dict = {'form': form}
    return render(request, 'testapp/register.html', context=my_dict)
...