Django找不到任何视图但索引

时间:2014-11-08 23:28:58

标签: python django django-views

我对Python和Django也很陌生,但是我尽可能地仔细阅读教程,但我必须遗漏一些东西。我可以加载我的默认索引视图,但是我想向一个名为' search'的视图发送一个ajax调用。我有另一个名为“显示'这需要一个整数(show_id),我对这两个页面的调用都得到404.

我错过了什么?我的设置中还需要其他任何东西吗?我的索引视图工作正常,但这些其他路线并不适合。感谢您的帮助!

编辑: 这是我的文件夹结构的屏幕抓图:enter image description here

媒体管理/ urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mediamanager.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^manager', include('manager.urls')),
    url(r'^admin/', include(admin.site.urls))
)

设定:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'manager'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mediamanager.urls'

WSGI_APPLICATION = 'mediamanager.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

manager.urls.py:

from django.conf.urls import patterns, url

from manager import views

urlpatterns = patterns('',
    url(r'^$',views.index, name='index'),
    url(r'^(?P<show_id>\d+)/$', views.show, name='show'),
    url(r'^search/$', views.search, name='search')
)

views.py

from django.shortcuts import render
from django.http import HttpResponse
from manager.models import Show, Episode
import urllib2


# Create your views here.
def index(request):
    context = {'shows' : Show.objects.all().order_by('show_name')}
    return render(request, 'manager/index.html', context)


def show(request, show_id):
    s = Show.objects.get(show_id).order_by('-air_date')
    context = {'show': s, 'episodes': s.episodes}
    return render(request, 'manager/show.html', context)

def search(request):
    search_term = request.GET['show_search']
    search_results = urllib2.urlopen("http://thetvdb.com/3BBD3F7E1043129C/GetSeries.php?seriesname=%s" % search_term)
    return HttpResponse(search_results)

2 个答案:

答案 0 :(得分:4)

这是你的问题:

url(r'^manager', include('manager.urls')),

应该是

url(r'^manager/', include('manager.urls')),
#             ^ The missing /

现在,index的作用原因是,有一种模式匹配/manager

请注意:manager123/(#123是节目ID)或managersearch/

将是您应用程序中的有效URL(非404)。

答案 1 :(得分:0)

试试这个

url(r'^manager/', include('manager.urls')),

这应该有效