0

我真的很困惑为什么我收到Http404错误。为了更清楚这是我的代码:

我的应用名为书

视图.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 url conf 相关的链接有效,但 bookslug url 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 %}

我的图书应用程序的 by_language 模板

{% 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当指定的 slug 与数据库中的任何查询都不匹配时,我已经包含了一个 raise方法。我感到困惑的是,当我尝试切换 langslug 和 bookslug urlconf 时,与这些 url 之一相关联的链接有效,而另一个无效。

4

1 回答 1

0

根据您的网址,如果我重视它,即使他们有不同的看法,结果将是:

urlpatterns = patterns('',

    # http://localhost:8000/English/
    url(r'(?P<langslug>.*)/$', views.by_lang_slug, name='by_lang'),

    # http://localhost:8000/YourBook/
    url(r'(?P<bookslug>.*)/$', views.by_book_slug, name='by_book'),

    # http://localhost:8000/
    url(r'^$', views.index, name='book_gallery'),
)

你注意到了吗,它们具有相同的模式,所以第一个视图执行的是by_lang_slug. 因此,如果您更改顺序,则将首先执行另一个顺序。最好的办法就是拥有a unique url name.

urlpatterns = patterns('',

    # http://localhost:8000/lang/English/
    url(r'lang/(?P<langslug>.*)/$', views.by_lang_slug, name='by_lang'),

    # http://localhost:8000/book/YourBook/
    url(r'book/(?P<bookslug>.*)/$', views.by_book_slug, name='by_book'),

    # http://localhost:8000/
    url(r'^$', views.index, name='book_gallery'),
)

现在他们不一样了......

于 2013-04-11T03:21:56.117 回答