Django师生问答网站无法展示答案问题求助(附代码)
'function' object has no attribute 'answer' Error in Your Django App Hey there! Let's figure out why you're hitting that frustrating error. This message almost always means you're trying to access an answer attribute on something that's actually a function—not a Django model instance or object that has that attribute. Let's walk through the most common fixes based on the code files you mentioned:
1. First, Validate Your models.py
Make sure your question model has a proper answer field defined (not a misnamed method or incorrect field type). For example, if your model tracks student questions, it should look something like this:
from django.db import models class StudentQuestion(models.Model): student_name = models.CharField(max_length=100) question_text = models.TextField() # This field stores teacher responses (nullable until answered) answer = models.TextField(null=True, blank=True) submitted_at = models.DateTimeField(auto_now_add=True) def __str__(self): return f"Question from {self.student_name}"
If you had a method named answer instead of a field (like def answer(self): ...), that could cause confusion—but your specific error is far more likely tied to passing a function instead of an instance to your template.
2. Check Your views.py Logic
The most common culprit here is passing a function to your template instead of a model instance. Let's compare wrong and right approaches:
❌ Wrong: You're passing the query function itself, not a fetched instance
# This breaks—Question.objects.get is a function, not an actual question object def question_view(request): context = {'question': Question.objects.get} # Missing parentheses + filters! return render(request, 'posturi.html', context)
✅ Correct: Fetch a specific instance using get_object_or_404 or a properly executed query
from django.shortcuts import render, get_object_or_404 from .models import StudentQuestion def question_detail(request, question_id): # Pull the actual question instance from the database question = get_object_or_404(StudentQuestion, id=question_id) return render(request, 'posturi.html', {'question': question})
Double-check that you're always passing a model instance (not a class, raw function, or unexecuted queryset) to your template context.
3. Fix Your posturi.html Template
Make sure you're accessing answer on the correct instance variable. Avoid these mistakes:
❌ Wrong: Accessing answer on a function or model class
<!-- If 'question' here is a function instead of an instance, this throws your error --> <p>Answer: {{ question.answer }}</p> <!-- Or accidentally using the model class name instead of the instance --> <p>Answer: {{ StudentQuestion.answer }}</p>
✅ Correct: Access answer on the instance you passed in your view
<div class="question-card"> <h3>{{ question.question_text }}</h3> {% if question.answer %} <div class="answer-section"> <strong>Teacher's Response:</strong> <p>{{ question.answer }}</p> </div> {% else %} <p>This question hasn't been answered yet.</p> {% endif %} </div>
Quick Debug Trick
If you're still stuck, add a quick debug line in your view to confirm what you're passing:
def question_detail(request, question_id): question = get_object_or_404(StudentQuestion, id=question_id) print(type(question)) # Should output <class 'yourapp.models.StudentQuestion'> return render(request, 'posturi.html', {'question': question})
If this prints something like <class 'function'>, you know you're passing the wrong type of object to the template.
内容的提问来源于stack exchange,提问作者soricaus

