问题标签 [django-class-based-views]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
515 浏览

django - 如何在基于类的通用视图中传递具有额外上下文的用户配置文件数据?

我有模型:

用户资料:

和 urls.py 中基于类的通用视图:

如何将带有额外上下文的 InstructorProfile 级别传递给我的模板?

0 投票
1 回答
804 浏览

django - 用于处理 AJAX 响应的基于 django 类的视图

我正在考虑编写自己的基于类的视图以始终如一地处理 AJAX GET 和 AJAX POST(用于发布表单,这可能会返回验证错误)。在我这样做之前,我想确保我没有重新发明轮子。是否有模块/应用程序已经用于上述目的?

0 投票
1 回答
1023 浏览

python - 在 Django 中对抽象视图进行单元测试

有没有一种方法可以测试一个抽象(基于类)的视图,没有实际 urlconf 链接到它的视图?这方面的例子是 Django 本身附带的通用视图的单元测试。

0 投票
1 回答
508 浏览

django-middleware - Django 中间件 process_template_response 未触发

我有一个 TemplateView,它在 process_template_response 中间件中返回一个 TemplateResponse 对象,但后者永远不会被触发。

当我更改 process_response 中的中间件方法并在 TemplateResponse 上执行 render() 时,会触发中间件方法并渲染页面。

我需要哪些步骤来呈现 process_template_response 中的视图?

看法:

0 投票
1 回答
115 浏览

django-templates - How to add a path to the Template Loader depending of the current class based generic view's model in Django

I am using the include template tag inside a template of a class based generic view and I need to append a custom path created with the current model system name to the list of templates that Django is going to check looking for the included template.

I am trying to create a custom template loader, but it needs to access to the view, the request or the context in order to get the model, and I haven't found the way of reach this.

How do I do it?

0 投票
2 回答
1683 浏览

django - 如何为基于类的视图编写装饰器——基于视图对象的权限

现在我正在使用这个应用程序进行权限检查:django-rules

然而,它已经一年多没有更新了,并且没有用于“新”(自 django 1.3 以来)基于类的视图的装饰器。我希望能够像这样在 urls.py 中使用:

我不知道如何从装饰器的基于类的视图中获取对象。你们有什么想法吗?这是我到目前为止所拥有的:

提前谢谢了!

0 投票
1 回答
241 浏览

python - Django 基于类的视图 - 意外的装饰器行为

[编辑]所以看来我的代码工作正常,另一段代码+疲倦是问题[/编辑]。

我有一个装饰器,它可以简单地检查几个所需的请求键。

如果其中一个字段丢失,我预计 HTTP 403 错误请求状态代码,但是装饰器从不执行该代码。

我的视图文件的基本表示:

在浏览器中打开 ViewA 时,控制台输出如下:

我不明白为什么要执行 ViewB 的装饰器,以及为什么我的控制台中没有“哦,嗨”。有什么见解吗?

0 投票
1 回答
18858 浏览

python - mixin 的顺序如何影响派生类?

说,我有以下通过触摸相互重叠的混合dispatch()

如果我希望我的视图通过订单,请检查 A -> 检查 B,我的代码应该是MyView(FooMixin, BarMixin, View)还是MyView(BarMixin, FooMixin, View)

为什么我们总是把View它或其子类放在 mixins 之后?(我从阅读 django 通用视图的源代码中注意到了这一点,但我不知道它背后的基本原理,如果有的话)

0 投票
1 回答
4375 浏览

django - 如何在基于类的通用视图中访问 RequestContext?

我的 urls.py 中有这条路径:

现在我想在模板中检测页面是否为当前页面(这是用于菜单样式)。既不 工作{{ request.path }}也不{{ request.get_full_path }}在模板中工作。

我应该改用什么?

解决方案

为了request在模板中可用,我必须添加django.core.context_processors.requestTEMPLATE_CONTEXT_PROCESSORS. 这不是默认设置的(从 django 1.3 开始)。

0 投票
1 回答
6350 浏览

python - 使用带有命名 url 的 Django 的 RedirectView

我正在尝试将我的a_detail重定向到我的a_detail_slug网址。我想为此使用命名的 url,但我还没有成功,这是我尝试过的:

这意味着捕获任何具有有效 pk 的链接,并使用附加的填充符重定向到该页面。