我想在我的 Django 站点中有一个顶级 about 页面(例如:http://127.0.0.1:8000/about)
,它指向基于 about View 的类民意调查应用程序或顶级关于页面,但是我得到:
TypeError at /about/
__init__() takes 1 positional argument but 2 were given
MySite/Mysite/urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
# Examples:
url(r'^$', 'myPollSite.views.index', name='myPollSite_home'),
url(r'^about/$', 'myPollSite.views.AboutView', name='myPollSite_about'),
url(r'^polls/', include('polls.urls', namespace = "polls")),
url(r'^admin/', include(admin.site.urls)),
]
MySite/Mysite/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
class AboutView(generic.TemplateView):
template_name = "polls/about.html"
def index(request):
return HttpResponse("Hello, world. You're at myPollSite index.")
MySite/polls/template/polls/about.html:
<h1>About Page</h1>
<h2>Implemented with TemplateView</h2>
<p>
No Model Data can be retrieved with Template View
</p>
最佳答案
按照 documentation 中的建议,使用 as_view()
调用配置基于类的 View :
url(r'^about/$', AboutView.as_view(), name='myPollSite_about'),
应导入 AboutView
的位置:
from myPollSite.views import AboutView
关于python - Django 错误中关于 Page 的顶级信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007294/