如何在一个HTML页面Django中使用两个不同的函数

2024-04-25 23:24:42 发布

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

我想创建一个网站,在那里我可以列出所有创建的表单,也可以在同一页面中创建表单。但是我想不出来。首先,我尝试使用两个链接到同一HTML文件的类,但后来我发现这是错误的,然后我尝试使用get post和get_queryset函数将这两个类写在一个类中。但是现在我只能创建表单,如果我删除get函数,我可以列出创建的表单

非常感谢,下面是我的views.py和HTML

views.py

from django.shortcuts import render,redirect
from django.contrib.auth.decorators import login_required
from django.views import generic
from .models import PostModel
from .forms import PostForm


# Create your views here.
class PostList(generic.ListView):
    template_name = 'home.html'
    form_class=PostForm

    def get_queryset(self):
        return PostModel.objects.order_by('-created_on')

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, {'form': form})

    def post(self,request,*args, **kwargs):
        form=self.form_class(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
        return render(request,self.template_name,{'form':form})


class PostDetail(generic.DetailView):
    model = PostModel
    template_name = 'post_detail.html'

home.html

{% extends "base.html" %}
{%block content%}

<div class="container">
    <div class="row">
        <!-- Blog Entries Column -->
        <div class="col-md-6 mt-3 left mx-auto">
            {% for post in postmodel_list %}
            <div class="card mb-4 block">
                <a class="overlay" href="{% url 'post_detail' post.slug  %}"style="text-decoration:none"> </a>
                <div class="card-body inner">
                  <p style="text-align:right;float:right;margin-top:10px;" class="card-text text-muted h6"><a style="text-decoration:none" href="https://google.com">@{{ post.author }}</a> </p>
                  <h2 class="card-title">{{ post.title }}</h2>
                </div>
            </div>
            {% endfor %}
          </div>
        </div>

</div>


<div class="col-md-4 float-right ">
  <button style= "position: fixed; bottom:50px;" type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
 <div class="modal-dialog" role="document">
   <div class="modal-content">
     <div class="modal-header">
       <h5 class="modal-title" id="exampleModalLabel">New message</h5>
     </div>
     <div class="modal-body">
       <form method="post" style="margin-top: 1.3em;">
         {% csrf_token %}
         {{ form }}
         <div class="modal-footer">
           <button type="submit" class="btn btn-primary">Submit</button>
           <button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
         </div>
       </form>
     </div>
   </div>
 </div>
</div>




<style>
.card{
  box-shadow: 0 16px 48px #E3E7EB;
}
</style>

{%endblock content%}

Tags: textfromimportselfdivformgetstyle
1条回答
网友
1楼 · 发布于 2024-04-25 23:24:42

如果我正确理解了这个问题,您可以使用CreateView而不是ListView,并从get_context_data返回上下文中的帖子列表

class PostList(generic.CreateView):
    template_name = 'home.html'
    form_class=PostForm
    model = Post
    
    def get_context_data(self, **kwargs)
        context = super().get_context_data(**kwargs)
        context ['postmodel_list'] = PostModel.objects.order_by('-created_on')
        return context

相关问题 更多 >

    热门问题