我真的很困惑,爲什麼我收到Http404
錯誤。爲了更清楚我的繼承人代碼:Django http404錯誤urlconf混淆
我的應用程序名爲books
views.py
from django.shortcuts import render_to_response
from django.http import Http404
from django.template import RequestContext
from books.models import *
def index(request):
title = 'Book Gallery'
books = Book.objects.all().order_by('-id')
lang_list = Lang.objects.all().order_by('-lang')
template = 'books/index.djhtml'
context = {'books': books, 'title': title, 'lang_list': lang_list}
return render_to_response(template, context, context_instance=RequestContext(request))
def by_book_slug(request, bookslug):
slug = bookslug
try:
book = Book.objects.get(slug=slug)
except:
raise Http404
title = book.name
template = 'books/singlebook.djhtml'
context = {'book': book, 'title': title}
return render_to_response(template, context, context_instance=RequestContext(request))
def by_lang_slug(request, langslug):
filter = langslug
try:
language = Lang.objects.get(slug=filter)
except:
raise Http404
lang_list = Lang.objects.all().order_by('-lang')
books = Book.objects.filter(lang=language).order_by('-id')
title = language
template = 'books/by_language.djhtml'
context = {'books': books, 'title': title, 'filter': filter, 'lang_list': lang_list}
return render_to_response(template, context, context_instance=RequestContext(request))
urls.py inside my book app folder
from django.conf.urls import patterns, include, url
from books import views
urlpatterns = patterns('',
url(r'(?P<langslug>.*)/$', views.by_lang_slug, name='by_lang'),
url(r'(?P<bookslug>.*)/$', views.by_book_slug, name='by_book'),
url(r'^$', views.index, name='book_gallery'),
)
環節,涉及到langslug網址的conf作品,但這些鏈接的URL bookslug CONF不起作用。當我嘗試切換它們時,其中一個工作,另一個不工作。
我真的不知道這裏發生了什麼。任何幫助將是一個很大的幫助。謝謝。
我的書應用程序的索引模板
{% extends 'base.djhtml' %}
{% block title %} | Gallery{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css" />
{% endblock %}
{% block content_header %}
{% endblock %}
{% block content_body %}
<div class="row">
<div class="span3">
<strong>filtered by >
{% if filter %}
{{ filter }}
{% else %}
All
{% endif %}
</strong>
<ul class="nav nav-list">
<li class="nav-header">Filter</li>
<li class="nav-header
{% if not filter %}
active
{% endif %}
"><a href="/books/">All</a></li>
{% for list in lang_list %}
<li class="nav-header
{% if filter == list.slug %}
active
{% endif %}
">
<a href="{% url by_lang langslug=list.slug %}">{{ list.lang }}</a>
</li>
{% endfor %}
</ul>
</div>
<div class="span9">
{% for book in books %}
<div class="span3">
<a href="{{ book.book_cover.url }}">
<img alt="{{book.name}}" src="{{ book.thumbnail.url }}" />
</a>
<h4>{{book.name}}</h4>
<p>{{book.desc|truncatewords:15}}</p>
<a href="{% url by_book bookslug=book.slug %}" class="btn btn-mini">View more...</a>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
{% extends 'base.djhtml' %}
{% block title %} | Gallery{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css" />
{% endblock %}
{% block content_header %}
{% endblock %}
{% block content_body %}
<div class="row">
<div class="span3">
<strong>filtered by >
{% if filter %}
{{ filter }}
{% else %}
All
{% endif %}
</strong>
<ul class="nav nav-list">
<li class="nav-header">Filter</li>
<li class="nav-header
{% if not filter %}
active
{% endif %}
"><a href="/books">All</a></li>
{% for list in lang_list %}
<li class="nav-header
{% if filter == list.slug %}
active
{% endif %}
">
<a href="{% url by_lang langslug=list.slug %}">{{ list.lang }}</a>
</li>
{% endfor %}
</ul>
</div>
<div class="span9">
{% for book in books %}
<div class="span3">
<a href="{{ book.book_cover.url }}">
<img alt="{{book.name}}" src="{{ book.thumbnail.url }}" />
</a>
<h4>{{book.name}}</h4>
<p>{{book.desc|truncatewords:15}}</p>
<a href="{% url by_book bookslug=book.slug %}" class="btn btn-mini">View more...</a>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
時指定塞不匹配的任何查詢我已經包括加薪Http404
方法爲我的書應用程序的by_language模板數據庫。我感到困惑的是,當我嘗試切換langslug和bookslug urlconf時,與這些url之一關聯的鏈接不起作用。
在你的Django外殼,嘗試'Book.objects.get(塞=蛞蝓)'這裏蛞蝓是您正在使用測試值,看看有什麼發生。 – karthikr 2013-04-11 03:00:20
在附註中,'get_object_or_404(Book,slug = slug)'與'try..except'做同樣的事情 – karthikr 2013-04-11 03:00:58
謝謝你糾正我karthikr – 2013-04-11 03:14:32