Django Python" TypeError:view必须是可调用的,或者在include()"的情况下是list / tuple;与网址

时间:2018-01-27 19:34:24

标签: python django python-3.x django-views django-urls

我得到" TypeError:视图必须是可调用的,或者在include()"的情况下是list / tuple;对于Weather_App / urls.py

下面的订阅网址
import signupform.urls
import signupform.views as views

app_name = "Weather_App"
urlpatterns = [
    url(r'^admin/', admin.site.urls, name='admin'),
    url(r'^subscribe/', signupform.urls, name='signup'),
]

在我的其他网址文件signupform / urls.py中,我遇到了类似问题,但使用视图导入修复了

from django.conf.urls import url
from . import views as Weather_App_views

urlpatterns = [
    url(r'^$', Weather_App_views.index, name='index'),
    url(r'confirm/$', Weather_App_views.confirm, name='confirm'),
]

视图看起来像

from django.http import HttpResponse
import json
from urllib.request import urlopen
from django.template import loader
from django.shortcuts import get_object_or_404, render
from django.http import *
from django.core.exceptions import ValidationError
from django.db import IntegrityError

from .models import WeatherSubscription

# Create your views here.

def index(request):
    return render(request, 'signupform/index.html', {'location_set':WeatherSubscription.city_choices})

def confirm(request):
    ws = WeatherSubscription(email=request.POST['email_input'], location=request.POST['location_input'])
    render_kwargs = {}
    try:
        ws.clean_fields()
        ws.save()
    except ValidationError as e:
        render_kwargs['invalid_message'] = 'Email not in a standard form.'
    except IntegrityError as e:
        render_kwargs['invalid_message'] = 'Email already subscribed.'

    return render(request, 'signupform/confirm.html', render_kwargs)

模型只有一个WeatherSubscription函数

我的结构是 signedupform
- 的初始化
-apps
-models
-settings
-urls
-views
Weather_App
- 的初始化
-settings
-urls
-wsgi

1 个答案:

答案 0 :(得分:2)

如果要导入signupform.urls,则需要将应用程序中的url文件导入主应用程序

from django.conf.urls import url , include
from . import views as Weather_App_vie

urlpatterns = [
    url(r'^admin/', admin.site.urls, name='admin'),
    url(r'^subscribe/',include('signupform.urls'), name='signup'),
]