如何将基于函数的视图集成到基于类的视图中?

2024-09-28 20:58:01 发布

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

在我的站点中,我有两个用户部分。用户发布页面和配置文件页面。个人资料页面上有他们的所有信息,所以用户名、描述、名字/姓氏等,而用户帖子页面上有他们的所有帖子。然而,我想以某种方式将它们整合在一起

下面是一些代码

这是用户帖子的视图

class UserPostListView(ListView):
    model = Post
    template_name = 'mainapp/user_posts.html'
    context_object_name = 'posts'

    def get_queryset(self):
        user = get_object_or_404(User,username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-published_date')

如您所见,我正在返回特定用户的帖子

现在这是我的剖面图

def view_profile(request,pk=None):
        if pk:
            user_profile = User.objects.get(pk=pk)
        else:
            user_profile = request.user
        context = {'user':user_profile}
        return render(request,'mainapp/profile.html',context)

它返回用户的所有信息

下面是配置文件和用户帖子页面的HTML代码

{% block content %}


<div class="profile-page-container">

    <div class="profile-page-info">
<div class="profile-page-banner">
    <div class="profile-page-banner-background">
        <!-- <img src="https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569_960_720.jpg" alt="{{ user }}'s Background Image" > -->
    </div>
    <div class="profile-page-banner-text">
<h2 title="{{ user }}" id="username-profile-page">{{ user|safe|linebreaksbr|truncatechars_html:25 }} {% if user.userprofileinfo.verified %} <span class="image-profile-verified"><img draggable="false" title="Verified User" class="verifed" src="{% static 'images\verified.png' %}" alt="verified" width="25" height="25" srcset=""></span> {% endif %}</h2>

<p>{{ user.first_name }} {{ user.last_name }}</p><br>
</div>
</div>
<div class="profile-page-user-desc-box">
<p>{{ user.userprofileinfo.description }}</p>
<a href="{{ user.userprofileinfo.website }}"><p>{{ user.userprofileinfo.website }}</p></a>
<p>{{ user.userprofileinfo.joined_date |date:"F d Y" }}</p>
</div>
<br>
{% if user.userprofileinfo.image %}
<img class="rounded-circle account-img" src="{{ user.userprofileinfo.image.url }}" alt="{{ user }}'s Profile Picture'">
{% endif %}
</div>
<div class="user-post-user-profile-page">
    <h1 title="{{ user }}">Posts</h1>

{% for post in posts %}
<div class="content">
<div class="post">
        <h1 class='posttitle'>{{ post.title }}</h1>
        <img class="user-image" src="{{ post.author.userprofileinfo.image.url }}" alt="pfp" width="20%" height="20%">
                {% if post.published_date %}
                    <!-- <div class="postdate">
                        <i class="fas fa-calendar-day"></i> &nbsp; <p>Posted {{ post.published_date|timesince }} ago</p>
                    </div>   -->
                    <div class="posted-by">
                        <p>Posted by <strong>{{ post.author }}</strong>  {{ post.published_date|timesince }} ago</p>
                    </div>
                {% else %}
                    <a class="pub-post" href="{% url 'mainapp:post_publish' pk=post.pk %}">Publish</a>
                {% endif %}

              <p class='postcontent' >{{ post.text|safe|linebreaksbr }}</p>
                  </div>
                  </div>
{% endfor %}
</div>
</div>
{% endblock %}

这是user_posts

{% block content %}
<div class="sidebar">
        <p class="active" href="#">{{ view.kwargs.username }}</p>
        <button class="commentbtn"><a class="aclass" href="#">Connect with {{ view.kwargs.username }}</a></button>
        <p>{{ user.userprofileinfo.email }}</p>
        <p>Lorem</p>
      </div>


{% for post in posts %}
<div class="content">
<div class="post">
        <h1 class='posttitle'>{{ post.title }}</h1>
        <img class="user-image" src="{{ post.author.userprofileinfo.image.url }}" alt="pfp" width="20%" height="20%">
                {% if post.published_date %}
                    <!-- <div class="postdate">
                        <i class="fas fa-calendar-day"></i> &nbsp; <p>Posted {{ post.published_date|timesince }} ago</p>
                    </div>   -->
                    <div class="posted-by">
                        <p>Posted by <strong>{{ post.author }}</strong>  {{ post.published_date|timesince }} ago</p>
                    </div>
                {% else %}
                    <a class="pub-post" href="{% url 'mainapp:post_publish' pk=post.pk %}">Publish</a>
                {% endif %}

              <p class='postcontent' >{{ post.text|safe|linebreaksbr }}</p>
                  </div>
                  </div>
{% endfor %}
{% endblock %}

我试图通过剪切FBV并将其粘贴到get_queryset方法下方,将其合并到CBV中 就这样

def get_queryset(self):
    #... code here
def view_profile(request,pk=None):
    #... code here

然而,这并没有起作用。我只是好奇如何将两者结合在一起,这样我就可以在一个地方拥有两个世界中最好的


Tags: 用户imagedivimggetdatepageprofile