开发者

passing object data through URL

开发者 https://www.devze.com 2022-12-16 03:27 出处:网络
I know that I can pass object values through a URL pattern and use them in view functions. For instance:

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': 开发者_如何学JAVArecords })

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


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'),


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')


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消