我想在搜索结果中实现分页。搜索后我看到很好的结果(例如http://127.0.0.1:8001/search/?q=mos)
但是,当我单击“下一步”时,出现错误:
/search/ 处出现 ValueError,请求 URL: http://127.0.0.1:8001/ search/?city=2
不能使用 None 作为查询值
我认为问题出在网址(search_results.html)。我该如何修复它?我该如何改变:
<a href="/search?city={{ page_obj.next_page_number }}">next</a>
Run Code Online (Sandbox Code Playgroud)
模型.py
from django.db import models
class City(models.Model):
name = models.CharField(max_length=255)
state = models.CharField(max_length=255)
class Meta:
verbose_name_plural = "cities"
def __str__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
视图.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 …Run Code Online (Sandbox Code Playgroud) 我想从 django admin 下载数据作为 .csv 文件。我遵循教程https://www.endpoint.com/blog/2012/02/22/dowloading-csv-file-with-from-django。
我没有看到下载 csv 选项。我该如何解决我的问题?
我正在使用Python3,创建了迁移。
这是我的代码
模型.py
from django.db import models
from django.contrib import admin
class Stat(models.Model):
code = models.CharField(max_length=100)
country = models.CharField(max_length=100)
ip = models.CharField(max_length=100)
url = models.CharField(max_length=100)
count = models.IntegerField()
class StatAdmin(admin.ModelAdmin):
list_display = ('code', 'country', 'ip', 'url', 'count')
def download_csv(self, request, queryset):
import csv
f = open('some.csv', 'wb')
writer = csv.writer(f)
writer.writerow(["code", "country", "ip", "url", "count"])
for s in queryset:
writer.writerow([s.code, s.country, s.ip, s.url, s.count])
admin.site.register(Stat, StatAdmin)
Run Code Online (Sandbox Code Playgroud) 我想创建具有搜索和分页功能的应用程序。分页不适用于 ListView。
当我单击链接“下一步”时,我将从起始页http://127.0.0.1:8001/ ---> 移动到 http://127.0.0.1:8001/?city=2但列表的元素没有改变。
下次单击“下一个”链接不会更改网址(http://127.0.0.1:8001/?city=2 --> http://127.0.0.1:8001/?city=2)。
你能帮我找出错误吗? 我认为 *.html 文件中有错误,但找不到它
我的代码: models.py
from django.db import models
class City(models.Model):
name = models.CharField(max_length=255)
state = models.CharField(max_length=255)
class Meta:
verbose_name_plural = "cities"
def __str__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
urls.py
# cities/urls.py
from django.urls import path
from . import views
from .views import HomePageView, SearchResultsView
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'),
]
Run Code Online (Sandbox Code Playgroud)
视图.py
from django.shortcuts import render
from django.views.generic import TemplateView, ListView
from …Run Code Online (Sandbox Code Playgroud)