0

I used get_absolute_url in my app but it works only on admin site (by button view on site). On my website, the hyperlink is not responding. I've checked every line of code a couple of times and it seems like everything's fine. Does anyone know why it doesn't works?

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

from django.shortcuts import render from django.views import generic from .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

from django.urls import path, reverse

        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 %}

1 Answer 1

2

It should be

<a href="{{ crashreports.get_absolute_url }}">....</a>

not href="{{ crashreport.get_absolute_url }}" because you are using crashreports when iterating in for loop:

{% for crashreports in crash_reports_list %}
       ^^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.