Django URL with arguments pointing to the same page using "with as template"?

Baqir Khan

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

Neeraj Kumar

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.

edited at
0

Comments

0 comments
Login to comment

Related

Django - URL template tag with 2 arguments

how to get previous page url in django template?

Get page url and shorten link in Django Template

Different arguments with same template

Cannot properly generate URL with captured arguments using {% url %} template tag

Django different url going to the same page

Types of url arguments when linking via <a> tag in django template

display list value in template page using django

How to refresh an AsyncImage that is pointing to the same URL

Creating tkinter labels with links using a for loop results in all links pointing to the same URL. Why?

Using a variable for the app name {% url %} in Django template

Using Javascript to insert url into Django template

passing url parameter in django template using form

Django NoReverseMatch when using url in a template

Django get_absolute_url calling same URL as current page

Inheritance with same default template arguments

Django template. API request causes page to navigate to query url

Django - Unable to pass page title as url parameter in template

Django Page not found using complete url

Load another url into a html page using Jquery loads the same page

Redirect to Different Page While Using the Same HTML Template/File?

Indexing URL pointing to pdf using TIKA in SOLR

Django default arguments in URL

DJANGO MEDIA_URL returns to same page instead of showing image

Django - leaving url params the same after changing page

django template white page

Django template, send two arguments to template tag?

Showing listview and detailview in the same template using forloops in Django

Django DRY - how to simplify similar views using same template?