gpt4 book ai didi

Django - 如何让用户登录(ListView 中的 get_queryset)

转载 作者:行者123 更新时间:2023-12-03 12:22:21 26 4
gpt4 key购买 nike

模型.py:

from django.contrib.auth.models import User

class Location(models.Model):
user = models.ForeignKey(User)

View .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

网址.py:
url(r'^member/user_locations/$', UserLocationsListView.as_view(), name='user_locations'),

我希望用户能够在页面上看到他的所有位置。

似乎我在请求(在过滤和页面定义中)有问题

我应该如何解决这个问题?

谢谢!
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

最佳答案

您的问题与回溯完全无关。回溯显示您的 View 使用的模板中有一个无效字符(位置 748)。去掉它。

View 本身看起来不错。在方法中获取用户的正确方法是self.request.user ,正如您已经在做的那样。

您可以稍微简化该方法 - 您不需要在方法中进行分页, ListView会为你解决这个问题。

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

关于Django - 如何让用户登录(ListView 中的 get_queryset),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37299091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com