python Django ModelForm 模板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2281295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django ModelForm Template?
提问by dr.linux
I want to learn how can I add to template to my ModelForm i'm newbie. Below you can see my models.py, url.py and views.py:
我想了解如何将模板添加到我的 ModelForm 我是新手。下面你可以看到我的models.py、url.py和views.py:
My model.py looks like that:
我的 model.py 看起来像这样:
from django.db import models
from django.forms import ModelForm
from django.contrib.auth.models import User
class Yazilar(models.Model):
yazi = models.CharField(max_length=200)
temsilci = models.ForeignKey(User)
class YaziForm(ModelForm):
class Meta:
model = Yazilar
My views.py function is below:
我的 views.py 函数如下:
@login_required
def yazi_ekle(request):
yazim = YaziForm
return render_to_response('yazi/save.html', {'YaziForm': YaziForm})
My url.conf looks like below:
我的 url.conf 如下所示:
(r'^yazi/save/$', 'tryout.yazi.views.yazi_ekle'),
My question is about creating a form and what is that forms "action" parameter?
我的问题是关于创建一个表单,那个表单的“动作”参数是什么?
回答by Jj.
It seems to me that your problem is in the view, you should be doing something like this:
在我看来,您的问题出在视图中,您应该这样做:
@login_required
def yazi_ekle(request):
yazim = YaziForm() # Look at the (), they are needed for instantiation
return render_to_response('yazi/save.html', {'YaziForm': yazim}) # Sending the form instance to the context, not the form class
Now, you have a variable named YaziForm in your template context. Django forms autorender to a bunch of table rows with the widgets as default, so in your file yazi/save.html, do this
现在,您的模板上下文中有一个名为 YaziForm 的变量。Django 表单自动渲染到一堆表格行,默认使用小部件,因此在您的文件 yazi/save.html 中,执行此操作
<form method="post" action="">
{% csrf_token %}
<table>
{{YaziForm}}
</table>
<input type="submit" value="Submit Form"/>
</form>
That will render your form as a table automatically, though you have to add the logic for the form under POST.
这将自动将您的表单呈现为表格,但您必须在 POST 下添加表单的逻辑。
回答by Anentropic
You could in fact use <form action="">
since the url you want to post to is the same as the page you are on.
您实际上可以使用,<form action="">
因为您要发布到的网址与您所在的页面相同。
If you don't like that then as long as you have 'django.core.context_processors.request' in your TEMPLATE_CONTEXT_PROCESSORS in settings.py I think you could also do:
如果您不喜欢那样,那么只要您在 settings.py 的 TEMPLATE_CONTEXT_PROCESSORS 中有 'django.core.context_processors.request' 我认为您也可以这样做:
<form action="{{ request.path }}">
As always, see the docs :)
与往常一样,请参阅文档:)
http://docs.djangoproject.com/en/1.1/ref/request-response/#django.http.HttpRequest.path
http://docs.djangoproject.com/en/1.1/ref/request-response/#django.http.HttpRequest.path
EDIT
编辑
In case, in the intervening year since this question was posted, the poster still hasn't tried to read the ModelForm docs... http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/
以防万一,在此问题发布后的一年中,海报仍然没有尝试阅读 ModelForm 文档... http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/
Yes the view is wrong, you have instantiate the form. You also want some logic to handle the post data. If it's an edit view you probably also want the view to take an item id in the view args and have some logic to load that model instance.
是的视图是错误的,您已经实例化了表单。您还需要一些逻辑来处理发布数据。如果它是一个编辑视图,您可能还希望该视图在视图 args 中获取一个项目 ID,并有一些逻辑来加载该模型实例。
eg:
例如:
@login_required
def yazi_ekle(request, id=None):
form_args = {}
if id is not None:
# edit an existing Yazilar
try:
yazilar = Yazilar.objects.get(pk=id)
except Yazilar.DoesNotExist:
return Http404('Yazilar not found')
form_args['instance'] = yazilar
# else create new Yazilar...
if request.POST:
form_args['data'] = request.POST
yazi_form = YaziForm(**form_args)
if yazi_form.is_valid():
yazilar = yazi_form.save(commit=True)
else:
yazi_form = YaziForm(**form_args)
return render_to_response('yazi/save.html',
{
'yazi_form': yazi_form
},
context_instance=RequestContext(request)
)
then in your urls.py something like:
然后在你的 urls.py 中是这样的:
(r'^yazi/ekle/(?P<id>\d+)?$', 'tryout.yazi.views.yazi_ekle'),
and in the template:
并在模板中:
<form method="post" action="">
{% csrf_token %}<!-- required since Django 1.2 or later -->
<ul>
{{ yazi_form.as_ul }}
</ul>
<input type="submit" value="Submit Form"/>
</form>