J
J
Juvel19882021-02-10 01:30:35
Django
Juvel1988, 2021-02-10 01:30:35

How to properly process urls in Django so that the link works correctly?

Faced the following problem. I create a news resource. When I click on the link from the page with the news preview (news.html) to the page with the main article (article.html), I get a 404 error.

60230bd6a5181638981779.png

urls.py

path('new/<el_id>', views.el, name='el'),#Вывод отдельной новости


views.py
def el(request, el_id): #Вывод отдельной новости
    article = News.objects.get(id=el_id)
    entries = el.entry_set.order_by('-date_added')
    context = {'el':el, 'entries': entries}
    return render(request, 'prosvet_logs/article.html', context)


news.html

{% extends "prosvet_logs/base.html" %}
{% block content %}
<div class="News">
    <h2>Новости:</h2>
    {% if new %}
        {% for el in new %}
    <div class="articles">
        <h3><a href="/{{ el.id }}">{{ el }}</a></h3>
        <p>{{ el.anons }}</p>
    </div>
        {% endfor %}
    {% else %}
        <p>Новостей нет</p>
    {% endif %}
</div>
{% endblock content %}


article.html

{% extends "prosvet_logs/base.html" %}
{% block content %}

<div class="article">
    <h2>{{ el }}</h2>
    {% if new %}
        {% for en in entries %}
        <p>{{ en.date_added|date:'M d, Y H:i' }}</p>
        <p>{{ entry.body|linebreaks }}</p>
        {% endfor %}
    {% else %}
    <p>Новостей нет</p>
    {% endif %}

</div>
{% endblock content}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2021-02-10
@Juvel1988

The url is incorrectly specified in the template, it should be something like this: Documentation
<a href="{% url 'url_name' news_id %}">ololo</a>

V
Vladimir, 2021-02-10
@AstraVlad

In the urls it's worth 'new/<el_id>',but in the real url it's just /3, of course they don't match.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question