models.py:
from django.contrib.auth.models import User
class Location(models.Model):
user = models.ForeignKey(User)
views.py
class UserLocationsListView(ListView):
model = Location
context_object_name = 'user_locations'
def get_queryset(self):
user_locations = Location.objects.filter(user=self.request.user)
paginator = Paginator(user_locations, 10)
page = self.request.GET.get('page')
try:
user_locations = paginator.page(page)
except PageNotAnInteger:
user_locations = paginator.page(1)
except EmptyPage:
user_locations = paginator.page(paginator.num_pages)
return user_locations
urls.py:
url(r'^member/user_locations/$', UserLocationsListView.as_view(), name='user_locations'),
I want the user to be able to see all of his locations at the page.
It seems I have problems with REQUEST (while filtering and in page definition)
How should I fix this?
Thanks!
Environment:
Request Method: GET
Request URL: http://localhost:8000/member/user_locations/
Django Version: 1.8.6
Python Version: 2.7.11
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.instagram',
'allauth.socialaccount.providers.twitter',
'crispy_forms',
'findlocation_app')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "C:\commercial_projects\fl\lib\site-packages\django\core\handlers\base.py" in get_response
164. response = response.render()
File "C:\commercial_projects\fl\lib\site-packages\django\template\response.py" in render
158. self.content = self.rendered_content
File "C:\commercial_projects\fl\lib\site-packages\django\template\response.py" in rendered_content
133. template = self._resolve_template(self.template_name)
File "C:\commercial_projects\fl\lib\site-packages\django\template\response.py" in _resolve_template
88. new_template = self.resolve_template(template)
File "C:\commercial_projects\fl\lib\site-packages\django\template\response.py" in resolve_template
78. return loader.select_template(template, using=self.using)
File "C:\commercial_projects\fl\lib\site-packages\django\template\loader.py" in select_template
64. return engine.get_template(template_name, dirs)
File "C:\commercial_projects\fl\lib\site-packages\django\template\backends\django.py" in get_template
30. return Template(self.engine.get_template(template_name, dirs))
File "C:\commercial_projects\fl\lib\site-packages\django\template\engine.py" in get_template
167. template, origin = self.find_template(template_name, dirs)
File "C:\commercial_projects\fl\lib\site-packages\django\template\engine.py" in find_template
141. source, display_name = loader(name, dirs)
File "C:\commercial_projects\fl\lib\site-packages\django\template\loaders\base.py" in __call__
13. return self.load_template(template_name, template_dirs)
File "C:\commercial_projects\fl\lib\site-packages\django\template\loaders\base.py" in load_template
17. template_name, template_dirs)
File "C:\commercial_projects\fl\lib\site-packages\django\template\loaders\filesystem.py" in load_template_source
38. return fp.read(), filepath
File "C:\commercial_projects\fl\lib\codecs.py" in decode
314. (result, consumed) = self._buffer_decode(data, self.errors, final)
Exception Type: UnicodeDecodeError at /member/user_locations/
Exception Value: 'utf8' codec can't decode byte 0xcf in position 748: invalid continuation byte
Your question is completely unrelated to the traceback. The traceback shows that you have an invalid character (in position 748) in the template used by your view. Remove it.
The view itself looks ok. The correct way to get the user in the method is self.request.user
, as you are already doing.
You could simplify the method slightly - you don't need to do the pagination in the method, the ListView
will take care of that for you.
class UserLocationsListView(ListView):
...
paginate_by = 10
def get_queryset(self):
queryset = super(UserLocationsListView, self).get_queryset()
queryset = queryset.filter(user=self.request.user)
return queryset
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With