Noreversematch at /

Asked

Viewed 162 times

0

I’m taking some Python lessons with Django and is making this mistake:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

py.

from django.urls import path
from . import views

app_name = 'courses'

urlpatterns = [
  path('', views.index, name='index'),
  path('<str:slug>/', views.details, name='details'),
  path('<str:slug>/anuncios/', views.announcements, name='announcements'),
  path('<str:slug>/anuncios/<pk>', views.show_announcement, name='show_announcement'),
  path('<str:slug>/inscricao/', views.enrollment, name='enrollment'),
  path('<str:slug>/cancelar-inscricao/', views.undo_enrollment, name='undo_enrollment'),
  path('<str:slug>/aulas/', views.lessons, name='lessons'),
  path('<str:slug>/aulas/<pk>/', views.lesson, name='lesson'),
  path('<str:slug>/materiais/<pk>/', views.material, name='material'),

]

py views.

@login_required
@enrollment_required
def announcements(request, slug):
     course = request.course
     template = 'courses/announcements.html'
     context = {
        'course': course,
        'announcements': course.announcements.all(),
     }
    return render(request, template, context, slug)

@login_required
@enrollment_required
def show_announcement(request, slug, pk):
   course = request.course
   announcement = get_object_or_404(course.announcement.all(), pk=pk)
   form = CommentForm(request.POST or None)
   if form.is_valid():
      comment = form.save(commit=False)
      comment.user = request.user
      comment.announcement = announcement
      comment.save()
      form = CommentForm()
      messages.success(request, 'Seu comentário foi enviado com sucesso')
   template = 'courses/show_announcement.html'
   context = {
     'course': course,
     'announcement': announcement,
     'form': form,
  }
  return render(request, template, context, pk, slug)

show_annoucement.html

   {% extends "courses/course_dashboard.html" %}

   {% block dashboard_content %}
   {% for announcement in announcements %}
    <div class="well">
    <h2>
            <a href="{% url 'courses:show_announcement' Course.slug 
    announcement.pk %}">{{ announcement.title }}</a>
    </h2>
   {{ announcement.content|linebreaks }}
   <p>
           <a href="{% url 'courses:show_announcement' Course.slug 
    announcement.pk %}#comments">
             <i class="fa fa-comments-o" aria-hidden="true"></i>
             {% with comments_count=announcement.comments.count %}
             {{ comments_count }}       Comentário{{ comments_count|pluralize 
             }}
             {% endwith %}
           </a>
    </p>
 </div>
 {% empty %}
 <div class="well">
    <h2>Nenhum anúncio criado</h2>
 </div>
{% endfor %}
{% endblock %}

course_dashboard.html

{% extends "accounts/dashboard.html" %}

{% block breadcrumb %}
{{ block.super }}
   <li>/</li>
   <li><a href="{% url 'courses:announcements' course.slug %}">{{ course }} 
   </a></li>
{% endblock %}

{% block menu_options %}
  <li class="pure-menu-heading">
  {{ course }}
  </li>
  <li>
     <a href="{% url 'courses:lessons' course.slug %}">
         <i class="fa fa-video-camera"></i>
            Aulas e Materiais
     </a>
  </li>
 <li>
    <a href="#">
       <i class="fa fa-info-circle"></i>
       Informações
   </a>
</li>
<li>
   <a href="{% url 'courses:announcements' course.slug %}">
       <i class="fa fa-envelope"></i>
      Anúncios
  </a>

  • Forum of doubt
  • {{ block.super }} {% endblock %

    inserir a descrição da imagem aqui

    • Edit the question and add the template part base.html that is causing the error.

    • @drec4s there’s the base.html.

    • This part of the template has no URL for the show_annoucement view. I wanted to see was the part inside the block content view that causes the error.

    • @drec4s placed show_annnoucemnt.html and course_dashboard.html. I hope it’s one of those, because there are mitas inheritances in the templates.

    1 answer

    0

    I think the problem is related to the urls in show_annoucement.html. The variable course within the context Ourse is with lowercase "c":

    
       {% extends "courses/course_dashboard.html" %}
    
       {% block dashboard_content %}
       {% for announcement in announcements %}
        <div class="well">
        <h2>
          <a href="{% url 'courses:show_announcement' slug=course.slug pk=announcement.pk %}">
            {{ announcement.title }}
          </a>
        </h2>
       {{ announcement.content|linebreaks }}
       <p>
         <a href="{% url 'courses:show_announcement' slug=course.slug pk=announcement.pk %}#comments">
                 <i class="fa fa-comments-o" aria-hidden="true"></i>
                 {% with comments_count=announcement.comments.count %}
                 {{ comments_count }}       Comentário{{ comments_count|pluralize 
                 }}
                 {% endwith %}
               </a>
        </p>
     </div>
     {% empty %}
     <div class="well">
        <h2>Nenhum anúncio criado</h2>
     </div>
    {% endfor %}
    {% endblock %}
    
    • I fixed the C Course and now appears the whole code on the screen

    • What do you mean by code?

    • the page does not appear, but its code appears on the localhost

    • I put a screen print there in the post

    • That’s because you’re passing extra parameters inside the function surrender. You should just pass return render(request, template, context). (Slug and pk variables should not be included in render)

    Browser other questions tagged

    You are not signed in. Login or sign up in order to post.