Home > other >  Django - Reverse for 'topic' with arguments '('',)' not found. 1 patte
Django - Reverse for 'topic' with arguments '('',)' not found. 1 patte

Time:07-27

I'm learning Django and there was a problem. I will be grateful if you help

Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P[^/] )/\Z']

views:

def topic(request, topic):
    topic = New_topic.objects.get(id=topic)
    comments = topic.comment_set.order_by(' date')
    context = {'topic':topic, 'comments':comments}
    return render(request, 'topic.html', context)

urls:

from django.urls import path
from . import views
app_name = "forum"

urlpatterns = [
path('', views.main, name='main'),
path('<int:topic>/', views.topic, name='topic')
]

models:

from django.db import models
from django.contrib.auth.models import User

class New_topic(models.Model):
    text = models.CharField(max_length=64)
    date = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.text

class Comments(models.Model):
    topic = models.ForeignKey(New_topic, on_delete=models.CASCADE)
    text = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.text

template: main.html

{% block content %}
{% for list_forum in list %}
<a href="{% url 'forum:topic' topic.id  %}">{{ list_forum }}</a>
{% endfor %}
{% endblock content %}

topic.html

{% block content %}
  {{ topic }}
  {% for comment in comments %}
    {{ comment.text|linebreaks }}
  {% endfor %}
{% endblock content %}

CodePudding user response:

Try path('<topic>/<int:pk>/', views.topic, name='topic')

CodePudding user response:

I just reassigned the app url from "index" to "forum"

path('', include('forum.urls')) to change path('forum/', include('forum.urls'))

  • Related