如何将这两种观点合而为一?

时间:2019-05-31 23:57:19

标签: django django-views

我正在使用Django,并有一个profile.html,我试图使表单显示在表单的顶部和下方,该列表是该特定用户发表的帖子(分页)。无论如何,是否将 def配置文件(包括@login_required)视图的所有功能与类UserPostListView(ListView)合并到一个视图中?如何构造?

#1st View
@login_required
def profile(request):
    if request.method == 'POST':
        #pass instance of user that expects to alter
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated!')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form,
        'posts': request.user.post_set.all()
    }

    return render(request, 'users/profile.html', context)

#2nd View
class UserPostListView(ListView):
    model = Post
    #<app>/<model>_<viewtype>.html
    template_name = 'users/profile.html'
    context_object_name = 'posts'
    paginate_by = 5

    def get_queryset(self):
        #kwargs gives us query parameters / if they exist we get the user otherwise 404
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date_posted')

另一方面,这是我当前正在尝试实现的 profile.html

{% extends "forum/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <div class="media">
            <img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
            <div class="media-body">
                <h2 class="account-heading">{{ user.username }}</h2>
                <p class="text-secondary">{{ user.email }}</p>
            </div>
        </div>
        <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Profile Info</legend>
                {{ u_form|crispy }}
                {{ p_form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Update</button>
            </div>
        </form>
        {% 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" href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
                  <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
                </div>
                <!-- takes in post.id from the pk of the post in posts and sends this to post-detail-->
                <h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.question }}</a></h2>
                <p class="article-content">{{ post.answer|urlize }}</p>
              </div>
            </article>
        {% endfor %}
        {% if is_paginated %}
            {% if page_obj.has_previous %}
                <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
                <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
            {% endif %}

            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                    <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
                {% endif %}
            {% endfor %}

            {% if page_obj.has_next %}
                <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
                <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
            {% endif %}
        {% endif %}
    </div>
{% endblock content %}

0 个答案:

没有答案