I have links (which are list of the countries) when I click (for example) "USA" I get list of the cities from other countries too. But I only want the list of the cities in the USA.
Here is my github link. You are welcome to clone and play with it.
https://github.com/ualmaz/post
I tried to filter that like this. queryset = Post.objects.all().values_list( 'city', flat=True).distinct()
But it did not work.
This is my views.py
def cities(request):
queryset = Post.objects.all().values_list(
'city', flat=True).distinct()
context = {
'cities': queryset
}
return render(request, 'users/cities.html', context)
my models.py
class User(AbstractUser):
first_name = models.CharField(verbose_name="First name", max_length=255)
last_name = models.CharField(verbose_name="First name", max_length=255)
country = models.CharField(verbose_name="Country name", max_length=255)
city = models.CharField(verbose_name="City name", max_length=255)
email = models.EmailField(verbose_name="Email", max_length=255)
def __str__(self):
return self.username
class Post(models.Model):
title = models.CharField(max_length=255)
country = models.CharField(max_length=255)
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)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('users:blog')
my countries.html (it contains countries list)
{% extends 'shared/base.html' %}
{% load staticfiles %}
{% block content %}
<div class="content-section p-5 mt-5 pl-4">
<table class="table table-hover text-left col-sm-6" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<th>No: </th>
<th>Countries: </th>
</tr>
</tbody>
</table>
{% for post in posts %}
<table class="table table-hover text-left col-sm-6" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<td>{{ post.id }}</td>
<td><a href="{% url 'users:cities' %}">{{ post.country }}</a></td>
</tr>
</tbody>
</table>
{% endfor %}
{% endblock %}
</div>
This is my cities.html (This is where I am having the problem)
{% extends 'shared/base.html' %}
{% load staticfiles %}
{% block content %}
<div class="content-section p-5 mt-5 pl-4">
<table class="table table-hover text-left col-sm-12" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<th style="width: 200px;">No: </th>
<th> Cities: </th>
</tr>
</tbody>
</table>
{% for city in cities %}
<table class="table table-hover text-left col-sm-12" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<td style="width: 200px;">{{ post.pk }}</td>
<td>{{ city }}</td>
</tr>
</tbody>
</table>
{% endfor %}
{% endblock %}
</div>
Your problem is that you are not filtering by the country
that you clicked on. In your view, you should pass country
in as a parameter. You can then do Post.objects.filter(country=country).values_list('city', flat=True).distinct()
, which filters posts by country instead of getting all of the posts.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.