下午好!我正在尝试解决这个问题,但我自己解决它的所有尝试只会导致将 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个赞
您正在使用函数而不是基于类的视图。
基于功能
如果您使用的是一个函数,您基本上可以编写(为了约定,可以将函数更改为小写)。
path('', FormListView(), name = 'home'),
基于类
例如,如果您有一个基于类的视图,例如:
from django.views.generic import TemplateView
class AboutView(TemplateView):
#...
然后你可以像这样使用 as_view() :
path('about/', AboutView.as_view()),
添加回答
举报
0/150
提交
取消