Good afternoon! I am trying to solve this problem, but all my attempts to solve it myself have only resulted in changing def to class, and this does not help. Can you tell me what the problem is?
views.py
from django.core.mail import send_mail, BadHeaderError
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from .models import Form
def FormListView(request):
if request.method == 'GET':
form = FormListView()
else:
form = FormListView(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
surname = form.cleaned_data['surname']
email = form.cleaned_data['email']
try:
send_mail(name, surname, email, ['[email protected]'])
except BadHeaderError:
return HttpResponse('Invalid')
return redirect('success')
return render(request, "index.html", {'form': form})
def Success(request):
return HttpResponse('Success!')
urls.py
from django.urls import path
from .views import FormListView
urlpatterns = [
path('', FormListView.as_view(), name = 'home'),
path('success/', Success.as_view(), name = 'success')
]
errat:
File "/home/user/Portfolio/web_project/web_page/urls.py", line 5, in <module>
path('', FormListView.as_view(), name = 'home'),
AttributeError: 'function' object has no attribute 'as_view'
as_view()and just use FormListView. You want to write that in lower case since its a function and not a class. docs.djangoproject.com/en/3.0/topics/http/views for class based views: docs.djangoproject.com/en/3.0/topics/class-based-views/intro