django app

时间:2016-11-08 08:54:37

标签: python regex django url

我有网址,而且每个人都应该没问题,但我收到错误。

这是我的网址:

url(r'^/(?P<genre>%s)$' % '|'.join([g.code for g in Genre.objects.all()]), EventListView.as_view(), name='genre'),

以下是错误消息:

Reverse for 'genre' with arguments '()' and keyword arguments '{u'genre': ''}' not found. 0 pattern(s) tried: []

在这行HTML代码中:

<a {% if genre == view.genre %} class="active" href="{% url 'events' %}" title="{% trans 'Reset Filter' %}"{% else %} href="{% url 'genre' genre=genre.code %}"{% endif %}>{{ genre.name }}</a>

我的观点:

class EventListView(PageContextMixin, ListView):
    model = Booking
    page_context_kwargs = {'selected': reverse_lazy('events')}
    template_name = 'events/event_list.html'

    def get_queryset(self):
        filter_ = {'eventlist': True} # , 'season__in': settings.EVENTS_SEASONS}
        self.rooms = settings.EVENTS_ROOMS
        self.genres = list(settings.EVENTS_GENRES)
        genre_names = dict([(g.evis_id, g.description_de) for g in Genre.objects.filter(evis_id__in=[g['evis_id'] for g in self.genres])])
        for genre in self.genres:
            genre['name'] = genre_names.get(genre['evis_id'], genre['name'])
        if 'genre' in self.kwargs:
            self.genre = [g for g in self.genres
                          if g['url'] == self.kwargs['genre']][0]
            filter_['eventgenre__genre__evis_id'] = self.genre['evis_id']
        elif 'room' in self.kwargs:
            self.room = [r for r in self.rooms if r['url'] == self.kwargs['room']][0]
            filter_['venue_description'] = self.room['venue_description']
        return self.model.objects.filter(**filter_)

HTML模板:

<div id="eventlistcontainer">
    {% if view.genres or view.rooms %}
    <input type="checkbox" id="filteropener" class="hidden-input" />
    <div class="filterwrap">
        <label for="filteropener" class="filterlabel" title="{% trans "Toggle Filter" %}">{% trans "Filter" %}</label>
        {% if view.genre or view.room %}
        <span class="filtertag">
            : <b>{% if view.genre %}{{ view.genre.name }}{% else %}{{ view.room.name }}{% endif %}</b>
        </span>
        <a class="filterreset" href="{% url 'events' %}" title="{% trans 'Reset Filter' %}">
            <img src="{{ STATIC_URL }}images/icon_close.png" alt="{% trans 'Reset Filter' %}" />
        </a>
        {% endif %}
    </div>
    <div class="eventfilter">
        <div>
            <h2>{% trans "Genre" %}</h2>
            <ul class="linklist list1">
            {% for genre in view.genres %}
                <li>
                <a {% if genre == view.genre %} class="active" href="{% url 'events' %}" title="{% trans 'Reset Filter' %}"{% else %} href="{% url 'genre' genre=genre.code %}"{% endif %}>{{ genre.name }}</a>
                </li>
            {% if forloop.counter == 4 %}
            </ul>
            <ul class="linklist list2">
            {% endif %}
            {% endfor %}
            </ul>
        </div>
        <div>
            <h2>{% trans "Concert Venues" %}</h2>
            <ul class="linklist list3">
            {% for room in view.rooms %}
                <li>
                <a {% if room == view.room %} class="active" href="{% url 'events' %}" title="{% trans "Reset Filter" %}"{% else %} href="{% url "room" room=room.url %}"{% endif %}>{{ room.name }}</a>
                </li>
            {% if forloop.counter == 6 %}
            </ul>
            <ul class="linklist list4">
            {% endif %}
            {% endfor %}
            </ul>
        </div>
    </div>
    {% endif %}

    {% with object_list|sort_by_highlight_past as events %}
    <ul class="eventlist{% if events.0.past %} past{% endif %}">
        {% for event in events %}
        {% ifchanged event.past %}
        {% if not forloop.first %}
    </ul>
    <h3>Vergangene Veranstaltungen</h3>
    <ul class="eventlist past">
        {% endif %}
        {% endifchanged %}
        <li class="{{ event.status|default:"" }}{% if forloop.first and not event.past %} wide{% endif %}">
            {% for im in event.eventimage_set.all|slice:":1" %}
            <a href="{{ event.get_absolute_url }}" class="image">
                {% if forloop.parentloop.first and not event.past %}
                <img src="{{ im.image.image.3.url }}" alt="{{ im.text }}" />
                {% endif %}
                <img src="{{ im.image.image.2.url }}" alt="{{ im.text }}" />
            </a>
            {% endfor %}
            <div class="textbox">
                <a href="{{ event.get_absolute_url }}">
                    <p class="date">{{ event.start|fancy_date }}</p>
                    <h2>{{ event.name }}</h2>
                    <p class="location">{{ event.venue_description }}</p>
                </a>
                {% if event.description_short %}
                <p class="additional-info">{{ event.description_short }}</p>
                {% endif %}
                <a href="{{ event.get_absolute_url }}" class="button">{% trans "Details" %}</a>
                {% if event.status %}
                <p class="status">{% trans event.status %}!</p>
                {% elif event.selling_status_code == "EF" %}
                <a href="{{ event.get_absolute_url }}" class="button">{{ event.selling_status_description }}</a>
                {% elif event|ticket_link %}
                <a href="{{ event|ticket_link }}" class="button">{% trans "Tickets" %}</a>
                {% endif %}
            </div>
        </li>
        {% endfor %}
    </ul>
    {% endwith %}
</div>

流派

EVENTS_GENRES = (
    {
        'url': 'orchestermusik',
        'evis_id': '1',
        'name': 'Orchestermusik'
    },
    {
        'url': 'vokale-musik',
        'evis_id': '2',
        'name': 'Vokale Musik'
    },)

1 个答案:

答案 0 :(得分:1)

尝试将所有类型的网址作为选项放入网站是一个非常糟糕的主意。除了其他任何东西,查询只会被评估一次,因此URL只接受已在服务器启动时创建的类型。

相反,这是您在视图中检查的内容,例如get_object_or_404;它具有完全相同的结果,如果该类型不存在则返回404。

但是,在这种特殊情况下,错误是因为您传递给genre.code标记的{% url %}值由于某种原因而为空。但是,如果没有看到模板或视图的其余部分,我们无法告诉您原因。