python - 如何在一个 HTML 页面 Django 中使用两种不同的功能
问题描述
我想创建一个网站,我可以在其中列出所有创建的表单并在同一页面中创建表单。但我想不通。首先,我尝试使用两个链接到同一个 HTML 文件的类,但后来我读到这是错误的,然后我尝试使用 get post 和 get_queryset 函数将这两个类写成一个。但是现在我只能创建表单,如果我要删除 get 函数,我可以列出创建的表单。
非常感谢,这是我的 views.py 和 HTML。
视图.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'
主页.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%}
解决方案
如果我正确理解了这个问题,您可以使用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
推荐阅读
- javascript - Flow.js 从节点模块推断返回类型
- c++ - 在 Windows 操作系统上使用 C++ 检查特定的开放 TCP 端口
- c++ - 转换构造函数的参数可以隐式转换吗?
- python - 在较小表面内移动表面不会显示以前隐藏的组件
- ios - 如何在iOS中进行长时间的振动
- php - 如何使用 SSL 验证通过 Web 代理查看所有安全和不安全页面?
- excel - 我无法使用 VBA 将我的 Excel 工作表导出到 Mac 上的桌面
- ruby-on-rails - 如何在 Ruby 中创建新路径/路由,然后链接到它?
- replace - Notepad++ 搜索和替换:删除每行 N 之后的 3 到 4 个数字
- powershell - 为什么 ScriptBlock 中的命令不起作用?