gpt4 book ai didi

python - Django 找不到任何 View ,但索引

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:44 25 4
gpt4 key购买 nike

我也是 Python 和 Django 的新手,但我正在尽可能仔细地阅读本教程,我一定遗漏了一些东西。我可以让我的默认索引 View 加载,但我想发送一个 ajax 调用到一个名为“search”的 View ,我有另一个名为“show”的 View ,它需要一个整数(show_id),我在两个 View 上都得到 404s调用这些页面。

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

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

mediamanager/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'),
}
}

经理.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')
)

View .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)

最佳答案

这是你的问题:

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

应该是

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

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

请注意:manager123/(#where 123 is the show id)或managersearch/

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

关于python - Django 找不到任何 View ,但索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26823240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com