Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing object data through URL
    primarykey
    data
    text
    <p>I know that I can pass object values through a URL pattern and use them in view functions. For instance:</p> <pre><code>(r'^edit/(?P&lt;id&gt;\w+)/', edit_entry), </code></pre> <p>can be utilized like:</p> <pre><code>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}) </code></pre> <p>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 </p> <pre><code>href="/../specifications/{{ record.job_number }} </code></pre> <p>to pass the info to the url, but I already know that this regex syntax is incorrect:</p> <pre><code>(r'^specifications/(?P&lt;**job_number**&gt;\w+)/', display_specs), </code></pre> <p>nor can I capture the job_number in the view the same way I could an id:</p> <pre><code>def display_specs(request, job_number): records = SpecReport.objects.filter(pk=job_number) tpl = 'display.html' return render_to_response(tpl, {'records': records }) </code></pre> <p>Is there an easy approach to this or is it more complicated than I think it is?</p> <p>the amended code is as follows:</p> <pre><code>(r'^specdisplay/?agencyID=12/', display_specs), </code></pre> <p>and:</p> <pre><code>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 }) </code></pre> <p>not sure how to filter. pk is no longer applicable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload