get\u absolute\u url Django python出现问题

2024-06-28 06:15:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

型号.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)])

视图.py

从django.shortcuts导入渲染 从django.views导入generic 从.models导入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

网址.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'),

crashreport\u列表.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 %}

Tags: thetextnameselfindexobjectsmodelscontext
1条回答
网友
1楼 · 发布于 2024-06-28 06:15:46

应该是的

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

不是href="{{ crashreport.get_absolute_url }}",因为在for循环中迭代时使用的是crashreports

{% for crashreports in crash_reports_list %}
       ^^^^^^^^^^^^

相关问题 更多 >