Home > Blockchain >  How to access properties of one-to-Many relationships in Django templates?
How to access properties of one-to-Many relationships in Django templates?

Time:12-09

I am very new to Python & Django. I am building a personal diary with pretty simple properties such as Months and Entries.

models.py

class Month(models.Model):
    id = models.AutoField(primary_key=True)
    month_name = models.CharField(verbose_name="Month", max_length=20, unique=True)
    year_number = models.IntegerField(verbose_name="Year")
    featured_img = models.ImageField(verbose_name="Featured", upload_to='diary/featured_img', default=False)

    class Meta:
        verbose_name = "Month"
        ordering = ['-year_number', 'month_name']

    def __str__(self):
        return '%s, %s' % (self.month_name, self.year_number)


class Entry(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=200)
    content = models.TextField()
    month = models.ForeignKey('Month', on_delete=models.RESTRICT, null=True)
    img = models.ImageField(verbose_name="Image", upload_to='diary/entry_img', default=False)
    date_created = models.DateTimeField(verbose_name="Date", default=timezone.now)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "Entries"
        ordering = ['-date_created']

I am trying to build a simple webpage which displays the Months in list form and when I click on the month I get the entries in detail view. I completed the first step but I cannot figure out how to access the one-to-many relationships between Month and Entry model in Django templates. What I want to do is to display a page with all the Month model objects and when a user clicks on a specific month say "December", Django returns all the entries which have the month object defined as December.

This worked fine in the admin site where I inserted TabularInLine method and I could see all the entries for a specific month but I can't figure out how to do this in templates and views.

admin.py

class EntryInLine(admin.TabularInline):
    model = Entry


class MonthAdmin(admin.ModelAdmin):
    inlines = [EntryInLine]


admin.site.register(Month, MonthAdmin)


views.py

class MonthListView(generic.ListView):
    model = Month
    context_object_name = 'month_list'
    template_name = 'diary/month.html'


class MonthDetailView(generic.DetailView):
    model = Entry
    context_object_name = 'entries_list'
    template_name = 'diary/entries.html'


def index(request):
    month = Month.objects.all()
    entry = Entry.objects.all()

    context = {
        'month': month,
        'entry': entry,
    }

    return render(request, 'index.html', context=context)


urls.py

urlpatterns = [
    path('', views.index, name='index'),
    path('month/', views.MonthListView.as_view(), name='month'),
    path('month/<int:pk>', views.MonthDetailView.as_view(), name='entries'),
]


month.html

{% extends "base.html" %}

{% block title %}<title>Months</title>{% endblock %}

{% block content %}
<h1>Month</h1>
{% if month_list %}
    <ul>
        {% for month in month_list %}
        <li>
            <a href="month.get_absolute_url()">{{ month.month_name }}</a>
        </li>
        {% endfor %}
    </ul>
    {% else %}
    <p>There are no months to display</p>
    {% endif %}
{% endblock %}


entries.html

{% extends "base.html" %}

{% block title %}<title>Entries</title>{% endblock %}

{% block content %}
{% if entries_list %}
        {% for entry in entries_list %}
            <h3>{{ entry.title }}</h3>
            <p>{{ entry.date_created }}</p>
            <p>{{ entry.content }}</p>
        {% endfor %}
{% else %}
    <p>There are no entries in this month.</p>
{% endif %}
{% endblock %}

I know the problem is probably in adding Entry model in the DetailView and the url mappings but I couldn't find a good resource online to explain what exactly is wrong.

CodePudding user response:

I got it! Added a get_context_data function in the MonthDetailView and it's working now!

class MonthDetailView(generic.DetailView):
    model = Entry
    context_object_name = 'entries_list'
    template_name = 'diary/entries.html'

    def get_context_data(self, **kwargs):
        context = super(MonthDetailView, self).get_context_data(**kwargs)
        context['entries_list'] = Entry.objects.filter(month=self.kwargs['pk'])
        return context

CodePudding user response:

class MonthDetailView(generic.DetailView):
    model = Entry
    context_object_name = 'entries_list'
    template_name = 'diary/entries.html'

    def get_context_data(self, **kwargs):
        pk = kwargs.get('pk') # this is the primary key from your URL
        context = super(MonthDetailView, self).get_context_data(**kwargs)
        context['entries_list'] = Entry.objects.filter(month=pk)
        return context

I think this may help.

  • Related