Django:Url无法解决

时间:2017-07-20 08:02:09

标签: python django search

我已经制作了包含培训师列表的应用程序。我的索引视图从数据库中显示这些训练员配置文件我打算实施一个sear bar来过滤这些结果。我没有得到我做错的事。一旦我提到搜索行动中的网址,它就会显示反向匹配错误

url' s.py:

       #/trainer/
url(r'^$', views.IndexView.as_view(),name='index'),

#/trainer/<trainer_id>/
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(),name='details'),

#/trainer/trainer/add
url(r'trainer/add/$', views.TrainerCreate.as_view(), name='Trainer-add'),

#/trainer/trainer/<album_id>
url(r'trainer/(?P<pk>[0-9]+)/$', views.TrainerUpdate.as_view(), name='Trainer-update'),

#/trainer/trainer/add
url(r'trainer/(?P<pk>[0-9]+)/delete/$', views.TrainerDelete.as_view(), name='Trainer-delete'),


url(r'^search/$', views.search, name='Search'),

views.py

def search(request):
    query = request.GET['q']
    trainer= Trainer.objects.filter(name__icontains=query)
    return render(request,'trainer/index.html', {'trainer': trainer})

在我的基本模板中搜​​索表单

    <form class="navbar-form navbar-left" method="get" action="{% url 'trainer:Search' %}">
            <div class="form-group">
                <input type="text" id="searchBox" class="input-medium search-query" name="q" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Search</button>
    </form>

index.py

    <table style="width:100%" class="table table-hover">
                            <tr>
                                <th>#</th>
                                <th>Name</th>
                                <th>Technology</th>
                                <th>Location</th>
                            </tr>
                            {% for trainer in all_trainers %}
                                <tr>
                                    <td><input type="checkbox" id="trainer{{ forloop.counter }}" name="trainer" value="{{ trainer.id }}"></td>
                                    <td> <a href="{% url 'trainer:details' trainer.id %}"> {{ trainer.name }}</td>
                                    <td>{{ trainer.technology }}</td></a>

                                    <!-- View Details -->
                                    <td><a href="{% url 'trainer:details' trainer.id %}" class="btn btn-primary btn-sm">View Details</a></td>

                                    <td><a href="../media/{{ trainer.trainer_profile }}" class="btn">Download PDF</a></td>

                                    <!-- Delete Album -->
                                    <td>
                                        <form action="{% url 'trainer:Trainer-delete' trainer.id %}" method="post">
                                        {% csrf_token %}
                                        <input type="hidden" name="trainer_id" value="{{ trainer.id }}" />
                                        <button type="submit" class="btn btn-default btn-sm">
                                            <span class="glyphicon glyphicon-trash"></span>
                                        </button>
                                        </form>
                                    </td>
                                </tr>
                            {% endfor %}
                        </table>

3 个答案:

答案 0 :(得分:1)

您需要.then()而不是{% url 'Search' %}; {% url 'trainer:Search' %}适用于namespaced your urls

答案 1 :(得分:0)

更改此行

<form class="navbar-form navbar-left" method="get" action="{% url 'trainer:Search' %}">

<form class="navbar-form navbar-left" method="get" action="{% url 'Search' %}">

答案 2 :(得分:0)

你在某处包括urls.py吗?

否则你需要在正则表达式的开头添加插入符^。

url(r'^trainer/search/$', views.search, name='Search'),