# Methodology for creating a basic forum in Django

**URL:** <https://forum.djangoproject.com/t/methodology-for-creating-a-basic-forum-in-django/1916>\
**Category:** Using Django\
**Created:** [April 11, 2020, 9:43pm UTC](https://forum.djangoproject.com/t/methodology-for-creating-a-basic-forum-in-django/1916 "2020-04-11T21:43:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![valenciano8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/valenciano8/32/1062_2.png) [@valenciano8](https://forum.djangoproject.com/u/valenciano8)\
**Post date:** [April 11, 2020, 9:43pm UTC](https://forum.djangoproject.com/t/methodology-for-creating-a-basic-forum-in-django/1916/1 "2020-04-11T21:43:16Z")

</div>

Hi everyone !

In order to improve my Django skills, I decided to try to create a very basic forum app.

For the moment being I have three templates :

1. forums.html

2. topics.html

3. posts.html

My issue is with the second template “topics.html” :

Indeed, if you take a look at this section :

> ```
> <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>
> 
> ```

I’ve got an issue because if the _topic\_name_ is “Hello World”, this will result in :

```
localhost:8000/forums/topics/Hello World/

```

Which is not correct, what I would like is to automatically convert this to :

```
localhost:8000/forums/topics/hello-world/

```

Here is the full code for my models.py and views.py files :

**models.py**

```
class Topic(models.Model):
    topic_forum = models.ForeignKey(Forum, related_name="topic_forum", default=None, on_delete=models.CASCADE)
    topic_name = models.CharField(max_length=40)
    topic_description = models.CharField(max_length=80, blank=True)
    topic_author = models.ForeignKey(User, related_name="topic_author", on_delete=models.CASCADE)
    topic_creation_date = models.DateTimeField(auto_now_add=True)
    topic_answers = models.IntegerField(default=0)
    topic_last_answer_by = models.ForeignKey(User, related_name="topic_last_answer_by", on_delete=models.CASCADE)

    def __str__ (self):
        return self.topic_name

```

**views.py**

```
def forum_page(request, forum_name):
    template_path = os.path.join("forums","topics.html")
    forum = Forum.objects.get(forum_name__iexact=forum_name)
    topic_model = Topic.objects.filter(topic_forum=forum)
    processTopicName(topic_model)

    return render(request,template_path,{"forum":forum, "topic_model": topic_model})

```

In the above view, you can see that I have added a _processTopicName_ function :

```
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

```

But I don’t really know how to implement it the right way…

Thank you in advance for your help 😃

---

<div class="post-metadata">

**Author:** ![timonweb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/timonweb/32/570_2.png) [@timonweb](https://forum.djangoproject.com/u/timonweb)\
**Post date:** [April 13, 2020, 9:45am UTC](https://forum.djangoproject.com/t/methodology-for-creating-a-basic-forum-in-django/1916/2 "2020-04-13T09:45:26Z")

</div>

How does your `urls.py` look like?

The best way to solve your issue is to add `get_absolute_url` method to your method that returns the url to a single item. And the you could use it in a template:

```auto
<a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a>

```

More on `get_absolute_url` can be found in docs: [https://docs.djangoproject.com/en/3.0/ref/models/instances/#get-absolute-url](https://docs.djangoproject.com/en/3.0/ref/models/instances/#get-absolute-url)

---

<div class="post-metadata">

**Author:** ![valenciano8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/valenciano8/32/1062_2.png) [@valenciano8](https://forum.djangoproject.com/u/valenciano8)\
**Post date:** [April 13, 2020, 10:19am UTC](https://forum.djangoproject.com/t/methodology-for-creating-a-basic-forum-in-django/1916/3 "2020-04-13T10:19:46Z")

</div>

Hi,

Could you please delete this topic as this is a doublon  
Indeed, the real one is :

> [@Process a topic name for basic forum app](https://forum.djangoproject.com/t/process-a-topic-name-for-basic-forum-app/1920):
>
> 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}}"\>{{t…

Thank you in advance for your comprehension

(I wanted to send a Flag message but I got a 500 Internal error)
