TypeError:__ init __()需要1个位置参数但是2个被赋予django

时间:2018-06-05 18:57:10

标签: python django django-models django-urls

这是urls.py

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^/', include('main.urls', namespace='Home')),

]

这是views.py

from django.shortcuts import render,HttpResponse,render_to_response,HttpResponseRedirect
from django.views.generic import TemplateView
from main.models import *

class leaflet(TemplateView):
    template_name = "file.html"

当我编写此代码模板未显示时,会抛出错误:

  

__init__()需要1个位置参数,但有2个被赋予"

你能在我的代码中找到错误吗?

2 个答案:

答案 0 :(得分:0)

您需要在urlpatterns中添加leaflet.as_view()

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from main.views import leaflet

urlpatterns = [
   url(r'^admin/', include(admin.site.urls)),
   url(r'^/', include('main.urls', namespace='Home')),
   url(r'^test/', leaflet.as_view()),
]

答案 1 :(得分:-1)

urls.py应该看起来像

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^/', include('main.urls'), name='Home'),

]

更新: 如果您使用的是django-1.8或更早版本,则应在app_name函数中添加include参数

url(r'^/', include('main.urls', namespace='Home', app_name='polls'))