- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
功能性网站与教程中的通用 View 结合的速度之快给我留下了深刻的印象。此外,表单处理的工作流程也很好。我使用 ModelForm 帮助器类从我制作的模型中创建了一个表单,很高兴看到这么多功能结合在一起。当我使用通用的 list_detail.object_detail 时,我很失望我可以显示的所有字段都是单独的。我知道 ModelForm 类包含用于渲染的信息,所以我想使用带有通用 View 的 ModelForm。
我在 stackoverflow 上四处询问以获得一些指导,并感谢几位海报的答案和评论。我已经想出了如何让它工作,但在 DetailView 中有一个错误。该解决方案包括一种解决方法。
要将 ModelView 与通用 View 一起使用并让所有字段自动呈现以下工作:
创建一个项目,并在其中创建应用程序住院患者。
如果你有
# inpatients/models.py
class Inpatient(models.Model):
last_name = models.CharField(max_length=30)
first_name = models.CharField(max_length=30,blank=True)
address = models.CharField(max_length=50,blank=True)
city = models.CharField(max_length=60,blank=True)
state = models.CharField(max_length=30,blank=True)
DOB = models.DateField(blank=True,null=True)
notes = models.TextField(blank=True)
def __unicode__(self):
return u'%s, %s %s' % (self.last_name, self.first_name, self.DOB)
class InpatientForm(ModelForm):
class Meta:
model = Inpatient
# inpatients/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.views.generic import DetailView
from portal.inpatients.models import *
def formtest(request):
if request.method == 'POST':
form = InpatientForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/inpatients')
else:
form = InpatientForm()
return render_to_response("formtest.html", {'form': form})
class FormDetailView(DetailView):
model=Inpatient
context_object_name='inpatient' # defines the name in the template
template_name_field='inpatient_list_page.html'
def get_object(self):
inpatient=super(FormDetailView,self).get_object()
form=InpatientForm(instance=inpatient)
return form
def get_template_names(self):
return ['inpatient_list_page.html',]
#urls.py
from django.conf.urls.defaults import patterns, include, url
from django.views.generic import ListView
from portal.inpatients.models import Inpatient, InpatientForm
from portal.inpatients.views import FormDetailView
urlpatterns = patterns('',
(r'^formtest/$','portal.inpatients.views.formtest'),
(r'^inpatients/$', ListView.as_view(
model=Inpatient, template_name='inpatient_list_page.html')),
(r'^inpatient-detail/(?P<pk>\d+)/$', FormDetailView.as_view()),
)
# with a template containing
{% block content %}
<h2>Inpatients</h2>
<ul>
{% for aninpatient in object_list %}
<li><a href='/inpatient-detail/{{ aninpatient.id }}/'>
{{ aninpatient }}, id={{ aninpatient.id }}</a></li>
{% endfor %}
</ul>
{{ inpatient.as_p }}
{% endblock %}
# Yeah, kind of hokey. The template is for both the list view and detail view.
# Note how the form is rendered with one line - {{ inpatient.as_p }}
127 # The least-specific option is the default <app>/<model>_detail.html;
128 # only use this if the object in question is a model.
129 if hasattr(self.object, '_meta'):
130 names.append("%s/%s%s.html" % (
131 self.object._meta.app_label,
132 self.object._meta.object_name.lower(),
133 self.template_name_suffix
134 ))
135 elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
136 names.append("%s/%s%s.html" % (
137 self.model._meta.app_label,
138 self.model._meta.object_name.lower(),
139 self.template_name_suffix
140 ))
最佳答案
我相信你是对的。这是一个错误,源于 ModelForm 和 Models 都有一个 _meta 属性。任何时候从 get_object()
返回一个对象都会出现同样的错误。包含一个 _meta 属性。get_object
不必返回 Model 实例。您可以通过查看 DetailView
的来源来确认这一点。并阅读它的文档字符串:
class DetailView(SingleObjectTemplateResponseMixin, BaseDetailView):
"""
Render a "detail" view of an object.
By default this is a model instance looked up from `self.queryset`, but the
view will support display of *any* object by overriding `self.get_object()`.
"""
get_template_names method
的
SingleObjectTemplateResponseMixin
.
# The least-specific option is the default <app>/<model>_detail.html;
# only use this if the object in question is a model.
if hasattr(self.object, '_meta'):
names.append("%s/%s%s.html" % (
self.object._meta.app_label,
self.object._meta.object_name.lower(),
self.template_name_suffix
))
elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
names.append("%s/%s%s.html" % (
self.model._meta.app_label,
self.model._meta.object_name.lower(),
self.template_name_suffix
))
关于django - 将基于类的通用 View DetailView 与 ModelForm 一起使用揭示了一个错误 - 如何继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6564068/
我正在 Apple Developer 网站上查看适用于 iOS 的 SimpleDrillDown 示例:http://developer.apple.com/library/ios/#sample
我是新手 我的xcode版本是11.2.1,swift是5.1.2 当我在MasterView中使用NavigationView时,我有两个DetailView 我想在第一个DetailView关闭时
I am newbie on python django. when following Django Tutorial > Part04 > Generic View , i have troubl
我是 Django 的新手。有一个 html 页面 (project_details) 应该显示项目的标题和任务,但只显示项目的标题,而不是任务。任务存在,问题出在过滤器上!!! View .py 错
我有一个要显示为详细信息 View 的模型,我创建了一个 ListView ,该 ListView 有一个指向其详细信息 View 的链接。我没有收到任何错误,但模板没有呈现任何模型细节 链接到 De
我有点困惑,并希望利用 DetailView 功能使用外键作为我的过滤器来显示数据。基本上我的模型是这样的: class Category(models.Model): name = mode
我的模型事件有一个基于类的 DetailView,我想显示通过外键关联的类别条目。 模型.py class Event(models.Model): name = models.CharFie
我想返回用户相关记录。有人可以帮助我吗? 我的部分观点 class UserProfileDetailView(DetailView): model = get_user_model()
我正在尝试使用聚光灯结果引导用户到我的应用程序中的相应部分。 MainView 有几个按钮,允许转到应用程序的不同区域,其中之一是电话目录,位于它自己的 SplitViewController 内。
我正在尝试创建主细节 View 应用程序,但有点卡住了。 我有一个包含 100 个 DynamicPrototype Cells 的 tableView 显示 Bars,当我点击一个 cell 时,我
在应用程序中,我有两个模型,名为“类(class)”和“步骤”。每个步骤都属于一个类(class),每个类(class)又包含许多步骤。但是,我在为步骤创建详细 View 时遇到问题。例如,当我转到
我有 2 个模型,我得到了 IndexView使用 get_context_data 正常工作方法。然而我的 DetailView使用相同的技术是行不通的。我如何简单地将 2 个模型放入 Detail
我正在寻找一种优雅的方式来显示 Django 中所有可用的项目(不仅仅是选中的项目)DetailView , 同时对选中的样式进行样式化。 给定以下模型: class Topping(models.M
有没有简单的方法可以强制DetailView在 Yii2 中忽略其 attributes 中的这些字段列表,特别是 model是空的? 或者唯一的方法是定义 attributes 上的每个属性具有自己
我正在尝试在 View 文件中渲染图像。我的代码如下: $model, 'attributes' => [ 'id', 'name',
我收到以下错误: ImproperlyConfigured at /elearning/7447932a-6044-498a-b902-97cbdd0a4001/ DetailView is miss
在我的 DetailView 中,我想根据我的 url 中的 kwargs 获取对象,另外还检索它的所有相关(外键)值。 我用: queryset = Category.objects.select_
我正在制作一个将 PO 导出为 PDF 的采购订单系统,但我需要在上半部分显示来自买方和卖方的数据。我想并排放置 2 个 DetailView,每个都有 50% 的页面宽度。有可能的?到目前为止,我还
在浏览了几个谷歌搜索结果页面后,我仍然陷入同样的问题。我正在尝试在博客文章下方实现评论字段。我感谢任何提示和建议! 我正在 Django 中开发一个博客,该博客设置了第一个通用 ListView
我需要在 Django 中编写一个 DetailView。我实现了这个功能。但是,我需要添加更多数据以及上下文对象。我将如何实现这一目标。 我的一般观点是: class AppDetailsView(
我是一名优秀的程序员,十分优秀!