django 如何在同一个基于类的视图中传递两个模板

ppcbkaq5  于 2023-01-10  发布在  Go
关注(0)|答案(2)|浏览(177)

我有一个问题与django模板视图。
我有两个网址
1)对于当前用户配置文件
2)为了他的队友
urls.py

path('profile/', views.UserprofileIndex.as_view(), name='user-profile'),
path('profile/<int:pk>', views.TeamProfileIndex.as_view(), name='team-profile'),

是风景

class TeamProfileIndex(TemplateView):

template_name = 'accounts/profile/team_profile.html'

def get_context_data(self, **kwargs):
    context = dict()
    self.pk = self.kwargs.get("pk")
    try:
        user_profile = Profile.objects.get(user=self.pk)
    except Profile.DoesNotExist:
        logger.error("Requested user profile not available")
        raise Http404
    teams = User.objects.all()
    context.update(
        {
            'user_profile': user_profile,
            'teams' : teams
        }
    )
    return context

class UserprofileIndex(TemplateView):

template_name = 'accounts/profile/index.html'

def get_context_data(self, **kwargs):
    context = dict()
    try:
        user_profile = Profile.objects.get(user=self.request.user)
    except Profile.DoesNotExist:
        logger.error("Requested user profile not available")
        raise Http404

    teams = User.objects.all
    context.update(
        {
            'user_profile': user_profile,
            'teams' : teams
        }
    )
    return context

在这两个视图中,只有一个查询在更改,即“user_profile”。
有没有办法在一个视图中把它传递给两个不同的html页面?
我试过以下方法
类用户配置文件索引(模板视图):#模板名称= '帐户/配置文件/索引. html'

def initialize(self, *args, **kwargs):
    self.pk = None
    self.pk = kwargs.get('pk')

def get_template_names(self, *args, **kwargs):
    if self.pk:
        return 'accounts/profile/team_profile.html'
    else:
        return 'accounts/profile/index.html'

def get_context_data(self, **kwargs):
    context = dict()
    try:
        if self.pk:
            user_profile = UserProfile.objects.get(user=self.pk)
        else:
            user_profile = Profile.objects.get(user=self.request.user)
    except Profile.DoesNotExist:
        logger.error("Requested user profile not available")
        raise Http404

    teams = User.objects.all()
    context.update(
        {
            'user_profile': user_profile,
            'teams' : teams
        }
    )
    return context

但它不工作

lnxxn5zx

lnxxn5zx1#

这将工作。但我建议使用DetailView

class UserProfile(TemplateView):
    """
    User profile view
    """
    user_profile = None
    template_name = 'accounts/profile/index.html'
    team_profile_template_name = 'accounts/profile/team_profile.html'

    def get_user_profile(self):
        """
        Get user
        """
        if not self.user_profile:
            user_profile_pk = self.kwargs.get('pk')
            model, filter_kwargs = (UserProfile, dict(user=user_profile_pk)) \
                if user_profile_pk else (Profile, dict(user=self.request.user))
            self.user_profile = get_object_or_404(model, **filter_kwargs)
        return self.user_profile

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(dict(user_profile=self.get_user_profile(),
                            teams=User.objects.all()))
        return context

    def get_template_names(self):
        if self.kwargs.get('pk'):
            return [self.team_profile_template_name]
        return super().get_template_names()
bvjveswy

bvjveswy2#

如果你想在一个基于类的视图中使用两个模板,做一些类似于我为DetailView做的事情

class PollDetail(DetailView):
model = Poll
context_object_name = "poll"

不要在类中提及模板名称,而要在URL www.example.com中提及模板ulrs.py

path("result/<pk>",PollDetail.as_view(template_name="results.html")),
 path("vote/<pk>",PollDetail.as_view(template_name="vote.html"))

相关问题