如何在 Django 中使用通用视图?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
/_form.html
格式创建模板文件。将此模板放入设置文件中 TEMPLATE_DIRS 中定义的模板目录中。在模板文件本身中,您将需要使用标准 ModelForm 定义的上下文。
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.
我认为您不需要创建视图函数。您需要在模板目录中提供一个模板(在本例中为 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