如何在 Django 中使用通用视图?

发布于 2024-10-02 18:06:31 字数 599 浏览 2 评论 0原文

我正在尝试使用 Django 的通用视图来制作用户注册页面。我的应用程序的 urls.py 中有以下代码

from django.conf.urls.defaults import *
from models import Ticket

urlpatterns = patterns('',
    (r'^new/$', 'django.views.generic.create_update.create_object', { 'model': User } ),
)

现在,当我转到该 url 时,我收到以下信息:

TemplateDoesNotExist at /new/
auth/user_form.html

我已尝试制作一个模板来匹配它,但我不断收到该消息。有什么建议吗?

另外,我认为这将为我制作模板。当我制作模板文件时,我实际上在模板文件中放入了什么?

编辑:来自 Rails,我正在混合模板和视图。我仍然陷入困境,但我认为我需要在我看来创建一个功能。像这样的东西:

def user_form:
    #stuff

I am trying to use Django's generic views to make a user registration page. I have the following code in my app's urls.py

from django.conf.urls.defaults import *
from models import Ticket

urlpatterns = patterns('',
    (r'^new/

Now when I go to that url I get the following:

TemplateDoesNotExist at /new/
auth/user_form.html

I have tried making a template to match it but I keep getting that message. Any advice?

Also, I assumed that this would make the template for me. What do I actually put in the template file when I make it?

EDIT: Coming from rails I was mixing templates and views. I am still stuck but I think I need to make a function in my view. Something like:

def user_form:
    #stuff
, 'django.views.generic.create_update.create_object', { 'model': User } ), )

Now when I go to that url I get the following:

I have tried making a template to match it but I keep getting that message. Any advice?

Also, I assumed that this would make the template for me. What do I actually put in the template file when I make it?

EDIT: Coming from rails I was mixing templates and views. I am still stuck but I think I need to make a function in my view. Something like:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笙痞 2024-10-09 18:06:31

您需要使用 /_form.html 格式创建模板文件。将此模板放入设置文件中 TEMPLATE_DIRS 中定义的模板目录中。

在模板文件本身中,您将需要使用标准 ModelForm 定义的上下文。

{{ form.name.label_tag }} {{ form.name }}

You will need to create the template file using the format <app_label>/<model_name>_form.html. Place this template into the template directory as defined in TEMPLATE_DIRS in your settings file.

In the template file itself, you will need to use the context as defined by the standard ModelForm.

{{ form.name.label_tag }} {{ form.name }}
我的影子我的梦 2024-10-09 18:06:31

我认为您不需要创建视图函数。您需要在模板目录中提供一个模板(在本例中为 auth/user_form.html,也是可配置的)。该模板需要包含创建新用户对象所需的字段。

请参阅 文档< /a> 为此。

另请查看这个类似讨论

i don't think you need to create a view function. you need to provide a template (auth/user_form.html in this case, is also configurable) in your templates directory. The template needs to contain the necessary fields to create the new user object.

see the docs for this.

ans also check out this similar discussion

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文