tags:

views:

34

answers:

3

hi i am fairly new to django and i want to update some of my contents in the django DB for this i have written a little view function. which looks like this

def status_change(request,id):
    instance = get_object_or_404(register,id=id)
    if request.method == 'POST':
       rform = registerForm(instance=instance, data=request.POST)
       if rform.is_valid():
          instance = rform.save()
          return render_to_response('home.html')
    else:
        rform = registerForm(instance=instance)
        return render_to_response('status_change.html',{'rform':rform}) 

i want to pass this "id" from template to the "view". my urls lopk like this

      (r'^status/(?P<id>\d+)$', views.status_change),

and when i use

      (r'^status/(?P<id>\d+)$', views.status_change, name ='status_change'),

it barks as syntax error on this line
and in my template i have

<a href = "/status/{{user.id}}">Change Status</a>

but this whole thing is not getting the id from the template/url am i making a mistake somewhere. any help will be greatly appreciated

Thanks in advance

+1  A: 

You given it nothing to get user from. Pass it a RequestContext.

And use {% url %}.

The syntax error comes from the fact that tuples cannot have named arguments. Perhaps you wanted url() instead.

Ignacio Vazquez-Abrams
any snippet or example please
MajorGeek
http://docs.djangoproject.com/en/dev/ref/templates/api/#id1
Ignacio Vazquez-Abrams
+1  A: 

If you need the id of the current user, you can also user django's request context processor http://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request, which enables you to access it through {{ request.user.id }}.

lazerscience
I strongly second the idea of using context processors for making "standard values" available across all of your views/templates. It really reduces the amount of redundant value passing. Checkout RequestContext - http://docs.djangoproject.com/en/dev/ref/templates/api/#id1
Peter Rowell
A: 

thanks for every one i got a simple way but i dont know is it a good practice or not instead of getting the user id from the template/urls i simpley put this thing in my code and it works fine

def status_change(request):
    instance = get_object_or_404(register,pk=request.user.id)
MajorGeek