下午好!我正在嘗試解決這個問題,但我自己解決它的所有嘗試只會導致將 def 更改為 class,這無濟于事。你能告訴我問題是什么嗎?views.pyfrom django.core.mail import send_mail, BadHeaderErrorfrom django.shortcuts import render, redirectfrom django.http import HttpResponse, HttpResponseRedirectfrom .models import Formdef 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, ['kirill_popov_000@mail.ru']) except BadHeaderError: return HttpResponse('Invalid') return redirect('success') return render(request, "index.html", {'form': form})def Success(request): return HttpResponse('Success!')urls.pyfrom django.urls import pathfrom .views import FormListViewurlpatterns = [ path('', FormListView.as_view(), name = 'home'), path('success/', Success.as_view(), name = 'success')]是錯的: 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'
1 回答

慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
您正在使用函數(shù)而不是基于類的視圖。
基于功能
如果您使用的是一個函數(shù),您基本上可以編寫(為了約定,可以將函數(shù)更改為小寫)。
path('', FormListView(), name = 'home'),
基于類
例如,如果您有一個基于類的視圖,例如:
from django.views.generic import TemplateView
class AboutView(TemplateView):
#...
然后你可以像這樣使用 as_view() :
path('about/', AboutView.as_view()),
添加回答
舉報
0/150
提交
取消