Y
Y
Your_House2019-11-13 13:47:21
Django
Your_House, 2019-11-13 13:47:21

How to make a link to an article like "News/id + slug"?

How to make a link to an article like "News/id + slug"? That is, so that at the beginning there would be id, and then immediately slug? I know how to do it with one of them, but I can’t combine it in any way.
(Just don't need id and slug to be separated by a slash(/), i.e. what you need to be 'in one'.
Django v. 2.2
Now I have in "urls.py":

from django.urls import path, re_path
from . import views

app_name = 'News'
urlpatterns = [
    path('', views.mainS, name = 'mainS'),
    path('/<slug:articleSlug>/', views.article_details, name = 'article_details'),
]

In "views.py"
from django.shortcuts import render
from .models import Article

def mainS(request):
    articles = Article.objects.order_by("article_datePub")[:10]
    return render(request, "News/News.html", {"articles": articles})

def article_details(request, articleSlug):
    return render(request, "News/article_details.html")

And in "News.html"
{% block main_block %} 
    <div class="block-main center-block-main clearfix">
        {% for article in articles %}
        <div class="block-new">
            <a href="{% url 'News:article_details' article.article_slug %}"><div class="new">
            <!-- <a href="/{{ article.article_slug }}/"><div class="new"> -->
                <img src="{{ MEDIA_URL }}{{ article.article_img.url }}" alt="">
                <div class="text-new">
                    <div class="text-newh1">
                        <h1 class="texth1">{{ article.article_title }}</h1>
                    </div>
                    <div class="text-newp">
                        {{ article.article_text }}
                    </div>
                </div>
            </div></a>
        </div>
        {% endfor %}
    </div>
{% endblock main_block %}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question