i'm trying to access my model using both method but one of them is not working in my view. i want when a user click on {{question.title}} in my home page to be able to see all the content of the post in vewQuestion template, it's successfully redirected me to the ViewQuestion template with no errors but the current user post contents doesn't appear in the ViewQuestion template. is anybody who can help me please.
my urls
path('view/<int:pk>/', views.viewQuestion, name='viewQuestion'),
the both method
my_list = get_object_or_404(Question, id=pk)
list_of_question = Question.objects.get(id=pk)
the url
<a href="{% url 'viewQuestion' question.id %}" style="text-decoration: none;">
the home template
<div >
<div >
{% for question in list_of_question reversed %}
<div >
<div >
<div >
<p >{{question.user}}</p>
</div>
<div >
<a href="{% url 'viewQuestion' question.id %}" style="text-decoration:
none;">
<p >{{question.title}}</p>
</a>
<p>Category: {{question.category}}</p>
</div>
</div>
</div>
{%endfor%}
</div>
</div>
the viewQuestion template
<div >
<div >
<h1>{{question.title}}</h1>
<hr>
</div>
<br>
<h3 style="font-family: arial;">{{question.body}}</h3>
<hr>
<br>
<h5>{{question.user}}</h5>
</div>
the views
@login_required(login_url='login')
def index(request):
query = request.GET.get('q', None)
list_of_question = Question.objects.all()
if query is not None:
list_of_question = Question.objects.filter(
Q(title__icontains=query) |
Q(category__icontains=query)
)
context = {'list_of_question':list_of_question}
return render(request, 'index.html', context)
def viewQuestion(request, pk):
my_list = get_object_or_404(Question, id=pk)
list_of_question = Question.objects.get(id=pk)
answers = Answer.objects.filter(post_id=list_of_question)
context = {'list_of_question':list_of_question, 'answers':answers, 'my_list':my_list}
return render(request, 'viewQuestion.html', context)
my models
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100, blank=False, null=False)
body = models.TextField(blank=False, null=False)
category = models.CharField(max_length=50, blank=False, null=False)
def __str__(self):
return str(self.user)
class Answer(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
answer = models.TextField(blank=False, null=False)
post = models.ForeignKey(Question, on_delete=models.CASCADE)
def __str__(self):
return str(self.user)
CodePudding user response:
In template viewQuestion
you are using question
variable
For example
{{question.title}},
but you are not passing it in context
in viewQuestion
view.
There is only list_of_question
, answers
and my_list
.
Replace list_of_question
key with question
and it should work:
def viewQuestion(request, pk):
my_list = get_object_or_404(Question, id=pk)
question = Question.objects.get(id=pk)
answers = Answer.objects.filter(post_id=list_of_question)
context = {'question':question, 'answers':answers, 'my_list':my_list}
return render(request, 'viewQuestion.html', context)