Hi everyone !
I’m imporving my Django skills through a basic forum app.
Here are my templates files :
-
forums.html (Will show all of the Forum models)
-
topics.html (Will show all of the Topic models according to the Forum ForeignKey)
-
posts.html (Will show all of the related *post to a Topic)
I’ve got one issue in the topics.html template.
Indeed :
<tbody>
{% for topic in topic_model %}
<tr>
<td><a href="{{topic.topic_name}}">{{topic.topic_name}}</a></td>
<td>{{topic.topic_description}}</td>
<td>{{topic.topic_author}}</td>
<td>{{topic.topic_answers}}</td>
<td>{{topic.topic_last_answer_by}}</td>
</tr>
{% endfor %}
</tbody>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<str:forum_name>/", views.forum_page, name="forum_page"),
path("topics/<str:topic_name>/", views.topic_page, name="topic_page")
]
My issue is that assuming we have a topic named “Hello World”, with the configuration above the URL will be :
localhost:8000/forums/topics/Hello World
and I would like to convert it as :
localhost:8000/forums/topics/hello-world
How can I process to implement the following function inside my template ? :
def processTopicName(topic_model):
#Process topic name
new_topic_model = []
for topic in topic_model:
topic_new_name = topic_name.replace(" ","-")
topic_new_name = topic_new_name.lower()
new_topic_model.append(topic_new_name)
return topic_new_name
Thank you in advance for your help