Я использовал 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 импорт универсальный из.models import 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
urls.py
из пути импорта django.urls, обратный
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'),
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 %}