网址未达到django中其他应用的特定视图

时间:2017-06-21 08:44:21

标签: django django-templates django-views

我想为我的博客项目创建一个搜索应用。以下是代码

此navbar.html包含搜索栏,该搜索栏包含在每个页面中作为模板。 下面的搜索表单将搜索查询转到url = / search /

<nav class="navbar navbar-toggleable-md navbar-inverse bg-primary">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">ViBlog</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav">
        <li class="nav-item active">
            <a class="nav-link " href="{% url 'posts:home' %}">Home <span class="sr-only">(current)</span></a>
        </li>
        <form class="form-inline my-0 my-lg-0 mx-5" method="GET" url="{% url 'search' %}">
            <input class="form-control mr-sm-2" type="text" id="search" name="q" placeholder="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit" id="search">Search</button>
        </form>
    </ul>
</div>

urls.py

from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from authentication import views as auth_views
from search import views as search_views

urlpatterns = [   
    url(r'^admin/', admin.site.urls),
    url(r'^login/$', auth_views.login_view, name='login'),
    url(r'^signup/$', auth_views.signup, name='signup'),
    url(r'^logout/$', auth_views.logout_view, name='logout'),
    url(r'^search/$', search_views.search, name='search'),
    url(r'^draceditor/', include('draceditor.urls')),
    url(r'^', include('blogs.urls', namespace='posts')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py

现在我只是测试搜索功能是否会在调用时运行。但它没有运行。

from django.shortcuts import render
from django.http import HttpResponse

def search(request):
    return HttpResponse("HERE")

请告诉我我做错了什么。提前谢谢你

1 个答案:

答案 0 :(得分:2)

您的表单应具有action属性,而不是url

<form class="form-inline my-0 my-lg-0 mx-5" method="GET" action="{% url 'search' %}">