通过 URL 传递对象数据

发布于 2024-08-17 02:05:36 字数 1651 浏览 8 评论 0原文

我知道我可以通过 URL 模式传递对象值并在视图函数中使用它们。例如:

(r'^edit/(?P<id>\w+)/', edit_entry),

可以这样使用:

def edit_entry(request, id):
        if request.method == 'POST':
                a=Entry.objects.get(pk=id)
                form = EntryForm(request.POST, instance=a)
                if form.is_valid():
                        form.save()
                        return HttpResponseRedirect('/contact/display/%s/' % id)
        else:
                a=Entry.objects.get(pk=id)
                form = EntryForm(instance=a)
        return render_to_response('edit_contact.html', {'form': form})

但是如何从 url 中的模型字段(“id”除外)传递值?例如,我有一个抽象基本模型,其字段“job_number”由子模型“OrderForm”和“SpecReport”共享。我想单击订单上的“job_number”并调用同一职位编号的规格报告。我可以创建一个

href="/../specifications/{{ record.job_number }}

将信息传递到 url,但我已经知道这个正则表达式语法不正确:

(r'^specifications/(?P<**job_number**>\w+)/', display_specs),

我也不能像获取 id 一样捕获视图中的 job_number:

def display_specs(request, job_number):
    records = SpecReport.objects.filter(pk=job_number)
    tpl = 'display.html'
    return render_to_response(tpl, {'records': records })

是否有一个简单的方法可以实现此目的比我想象的更复杂?

修改后的代码如下:

(r'^specdisplay/?agencyID=12/', display_specs),

and:

def display_specs(request, agencyID):
    agencyID= request.GET.get('agencyID')
    records = ProductionSpecs.objects.filter(pk=id)
    tpl = 'display_specs.html'
    return render_to_response(tpl, {'records': records })

不知道如何过滤。 pk不再适用。

I know that I can pass object values through a URL pattern and use them in view functions. For instance:

(r'^edit/(?P<id>\w+)/', edit_entry),

can be utilized like:

def edit_entry(request, id):
        if request.method == 'POST':
                a=Entry.objects.get(pk=id)
                form = EntryForm(request.POST, instance=a)
                if form.is_valid():
                        form.save()
                        return HttpResponseRedirect('/contact/display/%s/' % id)
        else:
                a=Entry.objects.get(pk=id)
                form = EntryForm(instance=a)
        return render_to_response('edit_contact.html', {'form': form})

But how do I pass a value from a model field (other than "id") in the url? For instance, I have an abstract base model with a field "job_number" that is shared by child models "OrderForm" and "SpecReport". I want to click on the "job_number" on the order form and call the Spec Report for that same job number. I can create an

href="/../specifications/{{ record.job_number }}

to pass the info to the url, but I already know that this regex syntax is incorrect:

(r'^specifications/(?P<**job_number**>\w+)/', display_specs),

nor can I capture the job_number in the view the same way I could an id:

def display_specs(request, job_number):
    records = SpecReport.objects.filter(pk=job_number)
    tpl = 'display.html'
    return render_to_response(tpl, {'records': records })

Is there an easy approach to this or is it more complicated than I think it is?

the amended code is as follows:

(r'^specdisplay/?agencyID=12/', display_specs),

and:

def display_specs(request, agencyID):
    agencyID= request.GET.get('agencyID')
    records = ProductionSpecs.objects.filter(pk=id)
    tpl = 'display_specs.html'
    return render_to_response(tpl, {'records': records })

not sure how to filter. pk is no longer applicable.

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

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

发布评论

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

评论(3

思念绕指尖 2024-08-24 02:05:36

是的,你让事情变得更加复杂了。

在您的 urls.py 中,您有:

(r'^edit/(?P<id>\w+)/', edit_entry),

现在您只需为 display_specs 添加几乎相同的表达式:

(r'^specifications/(?P<job_number>\w+)/', display_specs),

正则表达式中的括号标识一个组,并且 (?P...) 定义一个命名组,其名称为name。该名称是您视图的参数。

因此,您的视图现在看起来像:

def display_specs(request, job_number):
   ...

最后,即使这可行,当您重定向到视图时,不要使用:

HttpResponseRedirect('/path/to/view/%s/' % job_number)

使用 more DRY

HttpResponseRedirect(
    reverse('display_specs', kwargs={'job_number': a.job_number}))

现在,如果您决定更改资源路径,您的重定向将不会中断。

为此,您需要开始使用 命名网址你的 urlconf 像这样:

url(r'^specifications/(?P<job_number>\w+)/', display_specs, name='display_specs'),

Yes, you are making this a little more complicated that it is.

In your urls.py you have:

(r'^edit/(?P<id>\w+)/', edit_entry),

Now you just need to add the almost identical expression for display_specs:

(r'^specifications/(?P<job_number>\w+)/', display_specs),

Parenthesis in the regex identifies a group and the (?P<name>...) defines a named group which will be named name. This name is the parameter to your view.

Thus, your view will now look like:

def display_specs(request, job_number):
   ...

Finally, even though this will work, when you redirect to the view, instead of using:

HttpResponseRedirect('/path/to/view/%s/' % job_number)

Use the more DRY:

HttpResponseRedirect(
    reverse('display_specs', kwargs={'job_number': a.job_number}))

Now if you decide to change your resource paths your redirect won't break.

For this to work you need to start using named urls in your urlconf like this:

url(r'^specifications/(?P<job_number>\w+)/', display_specs, name='display_specs'),
旧夏天 2024-08-24 02:05:36

不知道你的模型结构是什么样的......为什么你不能直接传递特定作业的 id,然后通过查询来获取它?

Afaik 每个模型都会自动有一个自动递增的 id 字段,并且是行的唯一标识符(如果愿意的话,可以是索引),因此只需将 href 创建更改为 {{record.id}} 并从那里开始即可。

然后尝试通过 url 传递 job_number ,特别是如果您不太关心漂亮的 url 的话,只需这样做:

url:  /foo/bar/?job_number=12

没有特殊的标记来捕捉这个顺便说一句,正则表达式是 r'^foo/bar/'

然后在像这样查看:

job_number= request.GET.get('job_number')

Not knowing what your model structure is like ... why couldn't you just pass the particular job's id and then pick it up with a query?

Afaik every model automatically has an id field that autoincrements and is a unique identifier of a row (an index if you will), so just change the href creation to {{record.id}} and go from there.

Try passing the job_number through the url then, especially if you don't care about pretty url's too much just do this:

url:  /foo/bar/?job_number=12

no special markup to catch this btw, the regex is r'^foo/bar/'

And then read it in the view like this:

job_number= request.GET.get('job_number')
债姬 2024-08-24 02:05:36

我实在不明白你的问题。在 URL 中传递 id 和传递 job_number 之间有什么区别?如果你能做到其中之一,为什么不能做另一件事呢?一旦 job_number 出现在视图中,为什么不能进行正常的过滤:

records = SpecReport.objects.filter(job_number=job_number)

I really don't understand your question. What's the difference between passing id and passing job_number in a URL? If you can do one, why can't you do the other? And once the job_number is in the view, why can't you do a normal filter:

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