python - 分页中的网址错误。 Django

标签 python django

我想按两个字段(q1 和 q2)搜索: home.html

<form action="{% url 'search_results' %}" method="get">
    <input name="q" type="text" placeholder="Search...">
    <select name="q2" class="form-control" id="exampleFormControlSelect1">
        <option>All locations</option>
        <option>RU</option>
        <option>Ukraine</option>
        <option>USA</option>
    </select>
    <button> Search </button>
</form>

当我点击“搜索”时,我会转到 http://127.0.0.1:8001/search/?q=mos&q2=RU (没关系)

然后我单击“下一步”。我去

http://127.0.0.1:8001/search/?city=2&q=mos&q2=%20RU (ERROR: "Page not found)

但我想要

http://127.0.0.1:8001/search/?city=2&q=mos&q2=RU

我该如何修复它?为什么我有“%20”???

搜索结果.html

<h1>Search Results</h1>

<ul>
  {% for city in object_list %}
    <li>
      {{ city.name }}, {{ city.state }}
    </li>
  {% endfor %}
</ul>

<div class="pagination">
    <span class="page-links">
        {% if page_obj.has_previous %}
            <a href="/search?city={{ page_obj.previous_page_number }}&q={{ query }}&q2= {{query2}}">previous</a>

        {% endif %}
            <span class="page-current">
                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
            </span>
        {% if page_obj.has_next %}
            <a href="/search?city={{ page_obj.next_page_number }}&q={{ query }}&q2= {{query2}}">next</a>
        {% endif %}
    </span>
</div>

models.py

from django.db import models

class City(models.Model):
    name = models.CharField(max_length=255)
    state = models.CharField(max_length=255)
    COUNTRY = (
        ('RU', 'Russia'),
        ('UKR', 'Ukraine'),
        ('US', 'USA'),
    )
    category = models.CharField(max_length=100, choices=COUNTRY, default='RU')

    class Meta:
      verbose_name_plural = "cities"

    def __str__(self):
        return self.name

urls.py

urlpatterns = [
    path('search/', SearchResultsView.as_view(), name='search_results'),
    path('', HomePageView.as_view(), name='home'),
    path('city/<int:pk>/', views.city_detail, name='city_detail'),
]

views.py

class HomePageView(ListView):
    model = City
    template_name = 'cities/home.html'
    paginate_by = 3
    page_kwarg = 'city'

def city_detail(request, pk):
    city = get_object_or_404(City, pk=pk)
    return render(request, 'cities/city_detail.html', {'city': city})


class SearchResultsView(ListView):
    model = City
    template_name = 'cities/search_results.html'
    paginate_by = 3
    page_kwarg = 'city'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['query'] = self.request.GET.get('q')
        # added param
        context['query2'] = self.request.GET.get('q2')
        #print("BBBBBBBBBBBBBBBBb {}".format(context['query2']))
        return context

    def get_queryset(self): # new
        query = self.request.GET.get('q')
        query2 = self.request.GET.get('q2')
        #print("AAAAAAAAAAAAAAAAAAAAA", query2, type(query2))
        object_list = City.objects.filter(
            (Q(name__icontains=query) | Q(state__icontains=query)) & Q(category=query2)
        )
        return object_list

最佳答案

%20 是空间的 URL 代码。您需要删除 :

中的多余空格
<a href="/search?city={{ page_obj.previous_page_number }}&q={{ query }}&q2={{query2}}">previous</a>
#                                                                          ^                                                                 

<a href="/search?city={{ page_obj.next_page_number }}&q={{ query }}&q2={{query2}}">next</a>
#                                                                      ^             

关于python - 分页中的网址错误。 Django ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57933361/

相关文章:

python - 为什么我不能测试是否解决了正确的通用 View ?

django - 无法通过 AWS EC2 实例上的 Gunicorn 访问 Django 默认应用程序

django - 静态 CSS 和图像未在 Django 中加载

django - 如何在 Heroku 上安装 LaTeX 类?

python - “方法”对象不能通过从读取方法调用分割线来迭代

python - 使用 IIS 6 托管 Mercurial

Python:我可以有一个带有命名索引的列表吗?

python - 如何检查两个 XML 文档的等价性?

Python datetime模块无法更改变量参数

python - "pip freeze"给出了与 "help(' 模块不同的模块 ')"