pythondjangoslugify

Slugify a thread link and create it on the home.html


I am trying to slugify my hyperlinks. I created a Django board (home.html):

Currently I have:

http://127.0.0.1:8000/request/2/

But I want: http://127.0.0.1:8000/request/hallo-das-ist-ein-test-9/

enter image description here

  {% for topic in topics %}
    <tr>
      <td>{{ topic.slug }}</td>
       <td><a href="{% url 'topic_posts' topic.pk %}">{{ topic.subject }}</a></td>
      <td>{{ topic.starter.username }}</td>
      <td>0</td>
      <td>0</td>
      <td>{{ topic.last_updated }}</td>
    </tr>
  {% endfor %}

and

class Topic(models.Model):
    subject = models.CharField(max_length=255)
    category = models.CharField(max_length=255, null=True)
    last_updated = models.DateTimeField(auto_now_add=True)
    starter = models.ForeignKey(User, on_delete=models.CASCADE, 
related_name='topics')
    slug = models.SlugField(unique=True)

def save(self, *args, **kwargs):
    self.slug = slugify(self.subject)
    super(Topic, self).save(*args, **kwargs)

You can see, the slugs were created. (First column = {{ topic.slug }})

How can I create a slugified link and link them to my thread hyperlinks? (e. g. "hallo das ist ein test :9")

My current code in views.py:

def home(request):
    topics = Topic.objects.all()
    return render(request, 'home.html', {'topics': topics})


def topic_posts(request, topic_pk):
    topic = get_object_or_404(Topic, pk=topic_pk)
    return render(request, 'topic_posts.html', {'topic': topic})

My current url.py:

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^admin/', admin.site.urls),
    url(r'^new/$', views.new_topic, name='new_topic'),
    url(r'^request/(?P<topic_pk>\d+)/$', views.topic_posts, 
name='topic_posts'),
    url(r'^request/(?P<topic_pk>\d+)/reply/$', views.reply_topic, 
name='reply_topic'),

]

I already tried different approaches like:

def home(request):
    topics = Topic.objects.all()
    slug = Topic.slug
    return render(request, 'home.html', {'topics': topics, 'slug': slug})


def topic_posts(request, slug):
    slug = get_object_or_404(Topic, pk=slug)
    return render(request, 'topic_posts.html', {'slug': slug})

home.html:

<td><a href="{% url 'topic_posts' topic.slug %}">{{ topic.subject }}</a></td>

url.py:

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^admin/', admin.site.urls),
    url(r'^new/$', views.new_topic, name='new_topic'),
    url(r'^request/(?P<slug>[-\w\d]+)/$', views.topic_posts, 
 name='topic_posts'),
    url(r'^request/(?P<topic_pk>\d+)/reply/$', views.reply_topic, 
 name='reply_topic'),
]

What I am doing wrong here...?


Solution

  • There are several changes needed for your expected behaviour.

    In Table change:

      <a href="{% url 'topic_posts' topic.pk %}">
    

    to

     <a href="{% url 'topic_posts' slug=topic.slug %}">
    

    In views, update the code like this:

    def topic_posts(request, slug):
        topic = get_object_or_404(Topic, slug=slug)
        ...