Displaying ManyToManyField on Django Page

43 Views Asked by At

I'm trying to create a blog on Django, which on a blog post I can add one or more events and list them. For example: Blog Post A has Event1 and Event2 Blog Post B has Event 1 and Event 3

The main area I'm struggling with, is getting this code to actually show up on the page. I've tried a combination of things. The data shows up in the admin + database, but not on the front-end page

On the Model I'm showing the below

class Event(models.Model):
    eventname = models.CharField(max_length=150)
    location = models.CharField(max_length=150)
    eventdate = models.DateField(blank=False)
    eventlink = models.URLField(max_length=800)

class Post(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True)
    image = models.ImageField(upload_to=upload_location,
                              null=True,
                              blank=True,
                              width_field="width_field",
                              height_field="height_field")
    height_field = models.IntegerField(default=0)
    width_field = models.IntegerField(default=0)
    content = models.TextField()
    draft = models.BooleanField(default=False)
    publish = models.DateField(auto_now=False, auto_now_add=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    eventlist = models.ManyToManyField(Event)

On the HTML I'm showing the below

<h3> {{ post.eventlist.eventname }} </h3>

I expect the output to show the Event Name field on the page

0

There are 0 best solutions below