Django重定向不会附加到以前的模式

时间:2017-02-09 17:50:50

标签: python django forms django-forms

urls.py

 url(r'^reg/$', views.reg_view, name='reg'),
    url(r'^logout/$', views.logout_view, name="logout"),
    url(r'^search/$', views.search_redirect, name='search_redirect'),
    url(r'^search/(?P<keyword>.*?)/page/(?P<page>[0-9]+)/$', views.SearchView.as_view(), name='search'),
    url(r'^search/(?P<keyword>.*?)/$', views.SearchView.as_view(), name='search'),
    url(r'^t/create/$', views.create_topic, name='create_topic'),
    url(r'^notifications/$', views.NotificationView.as_view(), name='notifications'),


    url(r'^avatar/$', views.upload_avatar, name="upload_avatar"),

主要的urls.py中包含了这些(论坛版):

url(r'^accounts/', include('userena.urls')),
    url(r'^forum/', include(niji_urls, namespace="niji")),
    url(r'^$', index, name="index"),
    url(r'^despre-noi/$', despre_noi, name='desprenoi'),
    url(r'^(mugshots/.*)$', serve, {'document_root': settings.MEDIA_ROOT, }),
    url(r'^messages/', include('userena.contrib.umessages.urls')),
    url(r'^magazin/', include(application.urls)),
    # url(r'^magazin/', include('magazin.urls', namespace="magazin")),
    url(r'^oferte/', include('oferte.urls', namespace="oferte")),

表格

<form class="navbar-form navbar-left" role="search" method="get" action="{% url 'niji:search_redirect' %}">
                <div class="form-group">
                    <input type="text" id="keywordInput" name="keyword" class="form-control" placeholder="{% trans 'Search' %}">
                </div>
                <button type="submit" id="navSearchBtn" class="btn btn-default">{% trans 'Search' %}</button>
            </form>

Views.py

class SearchView(ListView):
    model = Topic
    paginate_by = 30
    template_name = 'niji/search.html'
    context_object_name = 'topics'

    def get_queryset(self):
        keywords = self.kwargs.get('keyword')
        query = get_query(keywords, ['title'])
        return Topic.objects.visible().filter(
            query
        ).select_related(
            'user', 'node'
        ).prefetch_related(
            'user__forum_avatar'
        ).order_by(
            get_topic_ordering(self.request)
        )

    def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        context['title'] = context['panel_title'] = _('Search: ') + self.kwargs.get('keyword')
        context['show_order'] = True
        return context


def search_redirect(request):
    if request.method == 'GET':
        keyword = request.GET.get('keyword')
        return HttpResponseRedirect(reverse('niji:search', kwargs={'keyword': keyword}))
    else:
        return HttpResponseForbidden('Post you cannot')

当用户使用表单提交搜索查询时,网址将变为example.com/search / ...而不是example.com/forum/search/ ..如果我手动输入url后者,它完美地工作,所以我认为它必须与表格有关?还是search_redirect函数?

1 个答案:

答案 0 :(得分:0)

显然,它与按钮有关。特别是我想的身份证。我不确切知道是什么......但删除它使它工作