简体   繁体   中英

Accessing a parent model attribute within Django template

I have an app that has a list of projects, and each project has a number of posts, and when you click on a project, it shows all of the posts in that project, but I can't figure out how to make it display what project the user is currently on, sample of it not working It should say "Posts for project 1" for example. Here is the relevant code of what I have so far.

    <h1 class="mb-3">Posts for {{ post.project.title }}</h1>
    {% for post in posts %}
        <article class="media content-section">
            <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
            <div class="media-body">
                <div class="article-metadata">
                    <a class="mr-2">{{ post.author }}</a>
                    <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
                </div>
                <h2><a class="article-title">{{ post.title }}</a></h2>
                <p class="article-content">{{ post.description }}</p>
            </div>
        </article>
    {% endfor %}
class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField(default='')
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    
    

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('project-detail', kwargs = {'pk': self.pk})

    class Post(models.Model):

    def get_default_action_status():
        return Project.objects.get(title="bruh")
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='posts', default=get_sentinel_exam_id)
    title = models.CharField(max_length=100)
    description = models.TextField(default='')
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    LOW = '!'
    MED = '!!'
    HIGH = '!!!'
    
    YEAR_IN_SCHOOL_CHOICES = [
        (LOW, '!'),
        (MED, '!!'),
        (HIGH, '!!!'),
    ]
    severity = models.CharField(
        max_length=3,
        choices=YEAR_IN_SCHOOL_CHOICES,
        default=LOW,
    )

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs = {'pk': self.pk})
    urlpatterns = [
    path('', ProjectListView.as_view(), name='blog-home'),
    path('user/<str:username>', UserProjectListView.as_view(), name='user-projects'),
    path('project/<str:title>', ProjectPostListView.as_view(), name='project-posts'),
    path('post/<int:pk>/', ProjectDetailView.as_view(), name='project-detail'),
    path('post/new/', ProjectCreateView.as_view(), name='project-create'),
    
    path('post/<int:pk>/update/', ProjectUpdateView.as_view(), name='project-update'),
    path('post/<int:pk>/delete/', ProjectDeleteView.as_view(), name='project-delete'),
    path('about/', views.about, name='blog-about'),
    ]
class ProjectPostListView(ListView):
    model = Post
    template_name = 'blog/project_posts.html'
    context_object_name = 'posts'
    paginate_by = 10
    
    def get_queryset(self):
        project = get_object_or_404(Project, title=self.kwargs.get('title'))
        return Post.objects.filter(project=project).order_by('-date_posted')

Apologies if some of that code wasn't needed, I am new to Django.

I figured it out by making the model of "ProjectPostListView" into a Project, and then changing that to a DetailView so I could just use all of the post objects associated with that project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM