我正在尝试使用queryset命令'Post.objects.values('country__name')'获取字段内容,并且出现以下错误。
NoReverseMatch at /
Reverse for 'cities' with arguments '('',)' not found. 1 pattern(s) tried: ['accounts\\/cabinet\\/cities\\/(?P<pk>[0-9]+)\\/$']
虽然在Django shell中键入此命令时没有出现任何错误。
我的观点:
def home(request):
user = User.objects.all()
cname = request.POST.get('dropdown1')
city = Post.objects.all().distinct('city')
country = Post.objects.values('country__name')
print(country)
context = {
'country': country,
'user': user,
'city': city
}
return render(request, 'registration/home.html', context)
我的模特:
class Post(models.Model):
title = models.CharField(max_length=255)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
city = models.CharField(max_length=255)
address = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
phone = models.CharField(max_length=255)
website = models.URLField(max_length=255, blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.city
def get_absolute_url(self):
return reverse('users:blog')
我的urls.py
urlpatterns = [
path('accounts/register/', UserRegistrationView.as_view(), name='register'),
path('accounts/cabinet/', views.profile, name='cabinet'),
path('accounts/cabinet/blog/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('accounts/cabinet/new/', PostCreateView.as_view(), name='post-create'),
path('accounts/cabinet/blog/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('accounts/cabinet/blog/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
path('', views.home, name='home'),
path('accounts/cabinet/blog/', views.blog, name='blog'),
path('accounts/cabinet/countries/', views.countries, name='countries'),
path('accounts/cabinet/cities/<int:pk>/', views.cities, name='cities'),
path('accounts/cabinet/address/<int:pk>/', views.address, name='address'),
]
答案 0 :(得分:0)
您有一个NoReverseMatch
error.,这意味着问题出在您的网址上。
它表示您正在尝试访问path('accounts/cabinet/cities/<int:pk>/', views.cities, name='cities'),
,但没有为其提供<int:pk>
参数。
检查您要加载的模板,并确保它在您的{% url 'cities' arg1 %}
块中包含该参数。