python - Django 错误中关于 Page 的顶级信息

标签 python django django-views django-class-based-views

我想在我的 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/

相关文章:

Mac OSX 10.6 上的 Python mysqldb 无法正常工作

python - 如何解码和验证 simple-jwt-django-rest-framework token

django - 当我不知道我将收到的结构时,如何循环遍历 Django 中的嵌套上下文字典

Django博客APPS

Python selenium webdriver 测试在 Windows XP 上大批量运行时导致 "serious error"

python - python中通过正则表达式替换特定字符串后面的数字

javascript - netlify 构建失败 : npm ERR! 路径/opt/build/repo/package.json

python - 如何从 Python 中的字符串中删除换行符和缩进?

python - 使用 openpyxl 将 'wrap_text' 应用于所有单元格

django - Django rest ModelViewSet过滤对象