get_absolute_url django python存在问题

get_absolute_url django python存在问题,python,django,Python,Django,我在我的应用程序中使用了get_absolute_url,但它仅在管理站点上有效(通过站点上的按钮查看)。在我的网站上,超链接没有响应。我已经检查了每一行代码好几次了,看起来一切都很好。有人知道为什么它不起作用吗 models.py class CrashReport(models.Model): number_id = models.AutoField(primary_key=True) date_notice = models.DateField(auto_now=False

我在我的应用程序中使用了get_absolute_url,但它仅在管理站点上有效(通过站点上的按钮查看)。在我的网站上,超链接没有响应。我已经检查了每一行代码好几次了,看起来一切都很好。有人知道为什么它不起作用吗

models.py

class CrashReport(models.Model):
    number_id = models.AutoField(primary_key=True)
    date_notice = models.DateField(auto_now=False, auto_now_add=False)

    SHIFT_NUMBER = (
        ('I', 'Zmiana I (6:00 - 14:00)'),
        ('II', 'Zmiana II (14:00 - 22:00)'),
        ('III', 'Zmiana III (22:00 - 06:00)'),

    )

    which_shift = models.CharField(
        max_length=3,
        choices=SHIFT_NUMBER,
        blank=True,
        default='I',
        help_text='Zmiana.'
    )

    who_notice = models.ManyToManyField(Author, help_text='Select a occupation for this employee')
    description = models.TextField(max_length=1000, help_text='Please type what happened')
    which_stuff = models.ManyToManyField(Device, help_text='Select a exactly devices')

    PROCESSING_STATUS = (
        ('o', 'Otwarty'),
        ('p', 'Przetwarzanie'),
        ('z', 'Zakonczony'),
    )

    status_notice = models.CharField(
        max_length=1,
        choices=PROCESSING_STATUS,
        blank=True,
        default='o',
        help_text='Status dokumentu.'
    )

    def __str__(self):
        return f'ZGL /{self.number_id} / {self.which_stuff.all()} / {self.date_notice}'

    def get_absolute_url(self):
        return reverse('crashreport-detail', args=[str(self.number_id)])
views.py

从django.shortcuts导入渲染 从django.views导入泛型 从。模型导入CrashReport

def index(request):
    """View function for home page of site."""

    # Generate counts of some of the main objects
    num_crashreports = CrashReport.objects.all().count()
    # num_instances = BookInstance.objects.all().count()

    # Opens Crashreports (status = 'o')
  #  num_crashreport_open = CrashReport.objects.filter(status__exact='o').count()

    context = {
        'num_crashreports': num_crashreports,
     #   'num_crashreports_processing': num_crashreports_processing,
     #   'num_crashreports_open': num_crashreports_open,
    }

    # Render the HTML template index.html with the data in the context variable
    return render(request, 'dokumenty/index.html', context=context)


class CrashReportsListView(generic.ListView):
    model = CrashReport
    context_object_name = 'crash_reports_list'   # your own name for the list as a template variable
    queryset = CrashReport.objects.filter()[:5] # Get 5 crash reports

class CrashReportsDetailView(generic.DetailView):
    model = CrashReport
url.py

从django.url导入路径,反转

        from . import views

        urlpatterns = [
            path('', views.index, name='index'),
            path('crashreports/', views.CrashReportsListView.as_view(), name='crashreports'),
            path('crashreport/<int:pk>', views.CrashReportsDetailView.as_view(), name='crashreport-detail'),
来自。导入视图
URL模式=[
路径(“”,views.index,name='index'),
路径('crashreports/',views.CrashReportsListView.as_view(),name='crashreports'),
路径('crashreport/',views.CrashReportsDetailView.as_view(),name='crashreport-detail'),
crashreport_list.html

{% extends 'baza.html' %}
{% block content %}

<h>Witam w Systemie Ewidencji Maria Awaria Service - Raporty</h>


  <h1>Zgłoszenia</h1>
  {% if crash_reports_list %}
  <ul>
    {% for crashreports in crash_reports_list %}
    <li>
        <a href="{{ crashreport.get_absolute_url }}">ZGL /{{ crashreports.number_id }} / {{ crashreports.which_stuff.all|join:", " }} / {{crashreports.date_notice}}</a>,

    </li>
    {% endfor %}
  </ul>
  {% else %}
    <p>There are no crash reports in the system.</p>
  {% endif %}



{% endblock %}
{%extends'baza.html%}
{%block content%}
Witam w Systemie Ewidencji Maria Awaria服务-Raporty
Zgłoszenia
{%if崩溃报告\u列表%}
    {对于崩溃报告中的崩溃报告,{u列表%}
  • ,
  • {%endfor%}
{%else%} 系统中没有崩溃报告

{%endif%} {%endblock%}
应该是

<a href="{{ crashreports.get_absolute_url }}">....</a>
{% for crashreports in crash_reports_list %}
       ^^^^^^^^^^^^