This is the weirdest problem I have ever come across. Here is it:
URLS.py
from django.conf.urls import url
from . import views
app_name = 'discussions'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>Q[0-9]+)$', views.question, name='question'),
]
Views.py
def index(request):
return render(request, 'discussions/discussion.html',
{"questions": Question.objects.all(), "activities": Activity.objects.all()[:10]})
#details for this function question are not neccessary but still I have added them.
def question(request, question_id):
question_id = int(question_id[1:])
question = get_object_or_404(Question, id=question_id)
questions = Question.objects.all()
related = []
answers = Answer.objects.filter(QID=question)
for q in questions:
score = (similar(q.title, question.title))
if score > 0.3 and score != 1:
related.append(q)
votes = Vote.objects.filter(username=User.objects.get(username=request.user.username)).filter(
Q(QID=question_id) | Q(AID__in=Answer.objects.filter(QID=question_id).values_list('id', flat=True)))
return render(request, 'discussions/question.html',
{"question": question, "votes": votes, "related": related, "answers": answers,
"activities": Activity.objects.all()[:7], "form": AnswerForm()})
Models.py
class Question(models.Model):
title = models.CharField(max_length=200 , default="")
description = models.CharField(max_length=1055, default="")
date = models.DateTimeField(default=datetime.now, blank=True)
username = models.ForeignKey(User, on_delete=None)
votes = models.IntegerField(default=0)
count = models.IntegerField(default=0)
def __str__(self):
return self.title
discussion.html
{% for question in questions|dictsortreversed:"votes" %}
<li class="list-group-item justify-content-between" data-toggle="tooltip" data-placement="top" title="asked by {{ question.username }} on {{ question.date }} has {{ question.count }} answers">
<a href="{% url 'discussions:question' with 'Q'|add:question.id as template %}" style="color: black"><h5>{{ question.id }}{{ question.title }}</h5></a>
<span class="badge badge-default badge-pill bg-{% if question.votes >= 0 %}success{% else %}danger{% endif %}">{{ question.votes }}</span>
</li>
{% endfor %}
Now the thing is in discussion.html on each question I want it to redirect it to its own question page which looks something like, website.come/discussion/Q5. Here 5 is the {{question.id}}. Now whenever I hover the mouse on the link, I am only redirected to the same page that is website.com/discussions
And the weirdest thing about this is that, whenever I open the page question.html, the all these things are working fine, the same code which is written in discussions.html works on that page!
How is it possible that I am doing same thing on both the templates and getting weird and irritating results?
Please feel free to ask me any extra information in the comments.
EDIT 1 However, if I change the URL pattern to
url(r'^(?P<question_id>[0-9]+)$', views.question, name='question'),
And change discussion.html link to:
<a href="{% url 'discussions:question' question.id %}" style="color: black"><h5>{{ question.title }}</h5></a>
Everything works perfectly and I m redirected to website.come/discussions/5
You are using wrong way in URL tag in html You defined in html
{% url 'discussions:question' with 'Q'|add:question.id as template %}
But {% url %} tag not use with
in url templatetag. see in documentation https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#url So when you defined this tag in html that not render any url in html therefore when you click on html it opening same url.
You can use two ways
1)
{% with question.id|stringformat:'s' as question_id %}
<a href="{% url 'discussions:question' question_id='Q'|add:question_id %}"></a>
{% endwith %}
2)
{% with question.id|stringformat:'s' as question_id %}
{% url 'discussions:question' question_id='Q'|add:question_id as template_url %}
<a href="{{ template_url }} ></a>
{% endwith %}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments