我仍然是Django的新手,慢慢搞清楚。虽然我遇到了这个错误,但我真的不知道从哪里开始调试它,因为追溯并没有告诉我这个位置。我想我必须查看views.py或models.py,然后找出nonetype与过滤器的位置,但之后,我不知道。
任何见解都将受到赞赏。
Request Method: GET
Request URL: http://127.0.0.1:8000/questions/by/test1/3/
Django Version: 1.11
Python Version: 3.5.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'accounts',
'posts',
'groups',
'questions',
'django_extensions']
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 "/Users/chrismaltez/anaconda3/envs/SimpleSocialEnv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/chrismaltez/anaconda3/envs/SimpleSocialEnv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/chrismaltez/anaconda3/envs/SimpleSocialEnv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/chrismaltez/anaconda3/envs/SimpleSocialEnv/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/Users/chrismaltez/anaconda3/envs/SimpleSocialEnv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/Users/chrismaltez/anaconda3/envs/SimpleSocialEnv/lib/python3.5/site-packages/django/views/generic/detail.py" in get
115. self.object = self.get_object()
File "/Users/chrismaltez/anaconda3/envs/SimpleSocialEnv/lib/python3.5/site-packages/django/views/generic/detail.py" in get_object
38. queryset = queryset.filter(pk=pk)
Exception Type: AttributeError at /questions/by/test1/3/
Exception Value: 'NoneType' object has no attribute 'filter'
以下是我的观点。问题的要点:
class QuestionDetail(generic.DetailView):
model = models.Question
def get_queryset(self):
queryset = super().get_queryset()
class UserQuestions(generic.ListView):
model = models.Question
template_name = "Questions/user_question_list.html"
def get_queryset(self):
try:
self.question_user = User.objects.prefetch_related("questions").get(
username__iexact=self.kwargs.get("username")
)
except User.DoesNotExist:
raise Http404
else:
return self.question_user.questions.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["question_user"] = self.question_user
return context
class CreateQuestion(generic.CreateView):
model = models.Question
# form = QuestionForm
fields = ('question', 'answer')
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return super().form_valid(form)
这是models.py要点:
class Quiz(models.Model):
name = models.CharField(max_length=225)
intro_text = models.TextField(null=True)
date_created = models.DateTimeField()
date_updated = models.DateTimeField(auto_now=True, null=True)
class Question(models.Model):
user = models.ForeignKey(User, related_name="question", default='')
quiz = models.ForeignKey(Quiz, related_name='quiz',default='', blank=True, null=True)
question = models.TextField(unique=False, default='')
question_html = models.TextField(default='')
date_created = models.DateTimeField(auto_now=True, null=True)
date_updated = models.DateTimeField(auto_now=True, null=True)
answer = models.TextField(default='')
answer_html = models.TextField(unique=False, default='')
# message = models.TextField(unique=False, default='')
# message_html = models.TextField(editable=False, default='')
def __str__(self):
return self.question
def save(self, *args, **kwargs):
self.question_html = misaka.html(self.question)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse(
"questions:single",
kwargs={
"username": self.user.username,
"pk": self.pk
}
)
class Meta:
# ordering = ["-created_at"]
unique_together = ["user", "question", "answer", ]
答案 0 :(得分:0)
我认为你需要做
def get_queryset(self):
queryset = super().get_queryset()
return queryset