我正在使用Django 1.10。我有以下设置:
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'IFTHQ',
'rest_framework'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Views.py:
from django.shortcuts import render
from IFTHQ import models
from django.contrib.auth.decorators import login_required
# Create your views here.
def home(request):
"""A view of all bands."""
return render(request, 'IFTHQ/publicpages/front.html')
@login_required(login_url="login/")
def dashboard(request):
member = models.Member.objects.get(id=request.user.id)
data = {}
data["profile"] = member
return render(request, 'IFTHQ/dashboard/dashboard.html', {"data": data})
@login_required(login_url="login/")
def membership(request):
data = {}
data["profile"] = member
return render(request, 'IFTHQ/dashboard/membership.html', {"data": data})
我确认会员对象中有成员,但是,当我登录时,我收到此错误:
Environment:
Request Method: GET
Request URL: http://<<domain>>/
Django Version: 1.10.2
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'IFTHQ',
'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/arcee123/trekfed/trekfed/IFTHQ/views.py" in dashboard
14. member = models.Member.objects.get(id=request.user.id)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/db/models/query.py" in get
385. self.model._meta.object_name
Exception Type: DoesNotExist at /
Exception Value: Member matching query does not exist.
似乎它找不到request.user.id
中的成员。我错过了什么?感谢。
更新1: 会员型号:
class Member(models.Model):
User = models.OneToOneField(User, related_name='user')
FID = models.FloatField()
backup_email = models.CharField(max_length=512)
FName = models.CharField(max_length=100)
MName = models.CharField(max_length=100, blank=True)
LName = models.CharField(max_length=100)
Avatar = models.FileField(upload_to='Avatar/%Y/%m/%d', blank=True)
Cover = models.FileField(upload_to='Cover/%Y/%m/%d', blank=True)
Facebook = models.URLField(default='', blank=True)
GooglePlus = models.URLField(default='', blank=True)
Twitter = models.URLField(default='', blank=True)
Website = models.URLField(default='', blank=True)
Bio = models.TextField(default='', blank=True)
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
Gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
Hair_Choices = (
('Blond', 'Blond'),
('Black', 'Black'),
('Brown', 'Brown'),
('Brunette', 'Brunette'),
('Red', 'Red'),
('Auburn', 'Auburn'),
('Gray', 'Gray'),
('White', 'White'),
('Colorful', 'Colorful'),
('None', 'None'),
('N/A', 'Not Applicable')
)
Hair = models.CharField(max_length=25, choices=Hair_Choices)
Height = models.FloatField()
def __str__(self):
return self.FName + " " + self.LName
def shortrank(self):
return Rank.objects.filter(User=self.id).order_by("-date_promoted")[0].Rank.SName
def currentrank(self):
return Rank.objects.filter(User=self.id).order_by("-date_promoted")[0].Rank.LongName
def rank_img(self):
return Rank.objects.filter(User=self.id).order_by("-date_promoted")[0].Rank.image
def primary_address(self):
return MemberAddress.objects.get(AddressFK=self.id, Default=True)
def primary_phone(self):
return MemberPhone.objects.get(PhoneFK=self.id, Default=True)
def awards(self):
return Award.objects.filter(Member=self.id)
此屏幕截图显示连接确实存在。
更新2: models.py:
class Member(models.Model):
User = models.OneToOneField(User)
...<More>...
Views.py:
@login_required(login_url="login/")
def dashboard(request):
member = request.user.member
data = {}
data["profile"] = member
return render(request, 'IFTHQ/dashboard/dashboard.html', {"data": data})
新错误:
Environment:
Request Method: GET
Request URL: <<domain.com>>
Django Version: 1.10.2
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'IFTHQ',
'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/arcee123/trekfed/trekfed/IFTHQ/views.py" in dashboard
14. member = request.user.member
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/utils/functional.py" in inner
235. return func(self._wrapped, *args)
File "/home/arcee123/trekfed/env-trekfed/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py" in __get__
370. self.related.get_accessor_name()
Exception Type: RelatedObjectDoesNotExist at /
Exception Value: User has no member.
答案 0 :(得分:0)
request.user.id
为您提供用户表中的ID。如果您想要获取相关成员,那么您应该过滤user
字段,而不是id字段。
member = models.Member.objects.get(user=request.user.id)
如果从外键中删除related_name='user'
,则可以执行以下操作:
member = request.user.member
使用related_name='user'
,您可以执行member = request.user.user
。但是,这会让人感到困惑,因为向后关系会给你一个Member
,而不是User
。