Home > Enterprise >  How to link an ID generated URL to the <a href> tag
How to link an ID generated URL to the <a href> tag

Time:05-04

I am making my own portfolio website using Django.. So the idea is: Make posts containing websites that i've developed. All these posts will be displayed in a page called "Projects" and then you can access a single project to read about it. Every project i upload have an ID, so i used this code to acess each project page:

# Single project page
    path('projects/<int:post_id>/', views.project, name='project'),

Then in my PROJECTS page i have a simple(for now) HTML code to show all my projects and link them to the single project page. What i want to know is how to LINK this ID generated URL in this code:

<ul>
  {% for project in projects %}
    <li><a href="#">{{ project }}</a></li>
  {% empty %}
    <li>No project have been added yet.</li>
  {% endfor %}
</ul>

I tried using:

<li><a href="{% url 'main_portfolio/projects/<int:post_id>/' %}</a></li

And some other things but nothing i try is working.

CodePudding user response:

Maybe try <a href="{{ project.get_absolute_url }}">, or <a href="projects/{{ project.id }}/">

CodePudding user response:

You can work with the {% url … %} template tag [Django-doc]:

<li><a href="{% url 'project' project.pk %}">some text</a></li>
  • Related