将使用捕获的 url 参数创建的表单传递给通用 django 视图?

发布于 2024-08-15 17:15:54 字数 775 浏览 5 评论 0原文

这看起来应该是显而易见的,但我却找不到解决方案。通常我只会编写一个简单的视图函数,它会填充适当的表单并将其传递给视图,但解决方案感觉非常接近..

我有一个表单。我想使用在 url 中捕获的 object_id 实例化此表单,然后使用 extra_context 参数将其发送到我的模板。

我有这样的东西:

class AddProductForm(forms.Form): 
    product = forms.IntegerField()
    quantity = forms.IntegerField()

还有这样的:

url(r'^products/(?P<object_id>\d+)/$', 
    'django.views.generic.list_detail.object_detail',
    {'queryset': Product.objects.all(),
    'extra_context': {'form': AddProductForm({'product': <what?>, 'quantity': 1})},
    name='product_detail'),

有没有办法用捕获的 object_id 值替换上面的 ? (也许传入 extra_context 的一个聪明的可调用函数可以为我制作表单?)

This seems like it should be obvious, but the solution is eluding me. Normally I would just write a simple view function which would populate an appropriate form and pass it along to the view, but the solution feels so close ..

I have a form. I want to instantiate this form using an object_id that I've captured in the url, then send it through to my template using the extra_context parameter.

I have something like this:

class AddProductForm(forms.Form): 
    product = forms.IntegerField()
    quantity = forms.IntegerField()

and this:

url(r'^products/(?P<object_id>\d+)/

Is there a way to replace <what?> above with the captured value of object_id? (Maybe a clever callable passed in extra_context could make the form for me?)

, 'django.views.generic.list_detail.object_detail', {'queryset': Product.objects.all(), 'extra_context': {'form': AddProductForm({'product': <what?>, 'quantity': 1})}, name='product_detail'),

Is there a way to replace <what?> above with the captured value of object_id? (Maybe a clever callable passed in extra_context could make the form for me?)

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

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

发布评论

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

评论(1

皓月长歌 2024-08-22 17:15:54

恐怕你不能在 urlconf 中这样做。您提供的任何可调用对象都不能接受任何参数,因此您将无法获取 ?P 的值。

不过,您可以在自己的视图中重复使用通用视图,以减少必须编写的样板文件数量:

from django.views.generic.list_detail import object_details
from your.forms import AddProductForm
from your.models import Product

def about_pages(request, object_id=None):
    qs = Product.objects.all()
    f = AddProductForm({'product':object_id,'quantity':1})
    return object_details(request,queryset=qs,extra_context={'form':f},template='yourtemplate.html')

I'm afraid you can't do that in your urlconf. Any callables you supply can not accept any arguments, so you won't be able to get the value of ?P<object_id>.

You can however re-use the generic view in your own view to cut down on the amount of boilerplate you have to write:

from django.views.generic.list_detail import object_details
from your.forms import AddProductForm
from your.models import Product

def about_pages(request, object_id=None):
    qs = Product.objects.all()
    f = AddProductForm({'product':object_id,'quantity':1})
    return object_details(request,queryset=qs,extra_context={'form':f},template='yourtemplate.html')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文