updating data in a row

50 views
Skip to first unread message

sum abiut

unread,
Jan 8, 2015, 9:49:22 PM1/8/15
to django...@googlegroups.com

Hi,
i am trying to update data in a row from an existing database but i keep getting this error.
update_form() takes exactly 2 arguments (1 given)

can someone advise what i am missing here.


here are my code:
view.py

def update_form(request, id):
if request.method == 'POST':
a=newleave.objects.get(id=id)
form =leave_application(request.POST, instance=a)
if form.is_valid():
form.save()
return HttpResponseRedirect('successful.html')
else:
a=newleave.objects.get(id=id)
form = leave_application(instance=a)
return render_to_response('update_form.html', {'form': form},context_instance=RequestContext(request))

form.py

class leave_application(forms.ModelForm):
class Meta:
model =newleave
fields =('First_Name', 'Last_Name', 'department', 'position', 'leave_type', 'Specify_details', 'start_Date', 'end_date', 'total_working_days', 'username')


model.py

class newleave(models.Model):
    First_Name = models.CharField(max_length=45)
    Last_Name =models.CharField(max_length=45)
    department=models.CharField(max_length =45)
    position=models.CharField(max_length =45)
    leave_type =models.CharField(max_length=45)
    Specify_details=models.TextField(default="")
    start_Date =models.DateField(null=True)
    end_date=models.DateField(null=True)
    total_working_days=models.IntegerField(null=True)
    authorization =models.CharField(max_length=45)
    authorized_by=models.CharField(max_length=45,  default ="")
    remarks=models.TextField()
    authorizaion_date =models.DateField(null=True)
    Total_Leave_Left =models.IntegerField(default=20)
    username  =models.ForeignKey(User,  default =1)
    staff =models.ForeignKey(staff,  default =1)
    
    def __unicode__(self):
        return self.First_Name



update_form.html

<form action ="/update_form/" method="post">{%csrf_token%}
<table>
{{form.as_table}}
</table>
<br>
<input type="submit" name="submit" value="Save Record" > 


</form>


James Schneider

unread,
Jan 8, 2015, 10:20:12 PM1/8/15
to django...@googlegroups.com

Looks like you aren't sending enough arguments to your view from the URL dispatcher. What does your urls.py look like?

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y4bFqUymcHzSC97znJxitpZvb0XEEwZVhhRm_gkyD%3DFkg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

sum abiut

unread,
Jan 8, 2015, 10:51:05 PM1/8/15
to django...@googlegroups.com
hi James,
here is the url.py

   url(r'^update_form/$', 'eLeave.views.update_form'),



Vijay Khemlani

unread,
Jan 8, 2015, 11:21:21 PM1/8/15
to django...@googlegroups.com
You have two choices

1. Change the URL mapping and pass the "id" in the url

url(r'^update_form/(?P<id>\d+)/$', 'eLeave.views.update_form'), 

(then the url is something like /update_form/15/)

2, Change the view so that it only accepts the "request" argument

def update_form(request):

in that case you can pass the id in the POST body of the request


sum abiut

unread,
Jan 9, 2015, 12:25:45 AM1/9/15
to django...@googlegroups.com
Hi,
I have change the URL mapping to  url(r'^update_form/(?P<id>\d+)
/$', 'eLeave.views.update_form'),

but i am getting the error

Page not found (404)

any advise i am getting this error?


cheers


James Schneider

unread,
Jan 9, 2015, 12:46:47 AM1/9/15
to django...@googlegroups.com

What URL are you visiting and can you post the traceback?

sum abiut

unread,
Jan 11, 2015, 5:27:41 PM1/11/15
to django...@googlegroups.com
Hi James,

I am try to visit this url http://10.0.X.X:8000/update_form/



here is the traceback:


Request Method: GET
Request URL: http://10.0.x.x:8000/update_form/

Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'eLeave')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/var/www/html/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /update_form/
Exception Value: update_form() takes exactly 2 arguments (1 given)



thanks,





Vijay Khemlani

unread,
Jan 11, 2015, 7:39:28 PM1/11/15
to django...@googlegroups.com
If you changed the url mapping then you need to include the id of the newleave in the URL you are accessing, for example 


sum abiut

unread,
Jan 11, 2015, 8:58:39 PM1/11/15
to django...@googlegroups.com
Thanks very much Vijay its works. But it doesn't really solve my problem, because i want to be able to edit more than one rows in a table not just one row. any help will be very much appreciated.

Cheers,



sum abiut

unread,
Jan 11, 2015, 9:30:38 PM1/11/15
to django...@googlegroups.com
I want to be able to click on a row, update it and then click on another row update it and so on.
-

Edgar Gabaldi

unread,
Jan 11, 2015, 9:34:21 PM1/11/15
to django...@googlegroups.com
add or update multiple rows, maybe you should see a little about formsets[1].


sum abiut

unread,
Jan 11, 2015, 10:01:11 PM1/11/15
to django...@googlegroups.com
Basically my table structure look something of this type. I want to be able to allow users to click on edit and to make changes to to the table.  just wondering if for loop can do the trick? i am a bit confuse.

Update

First Name

Last Name

Position

Department

Leave Type


edit







edit







edit







edit









James Schneider

unread,
Jan 12, 2015, 12:27:41 AM1/12/15
to django...@googlegroups.com
You'll need two views to do this, one of them is the form, and that you already have written. Have you read through the tutorial? It explains how to use an FBV (which seem to be your preference, which is fine) to create the other view that lists one or more objects in a table format:


The first column would contain a link to /update_form/<id> where the <id> is the PK of the object you are displaying on that row (ie {{ object.id }}. You would specify the URL for the link something like this: <a href="{% url 'url_name' object.id %}">Edit</a>

The template in the tutorial shows everything in an unordered list, but the template can be easily modified to match the table structure you would like. 

I just noticed that you haven't assigned names to your URL's in urls.py. I highly recommend you name your URL's so that you can reference them in your templates easier than using the path to the views.

If you want advanced table functionality (such as sorting by column), I would look at the django-tables2 package. It does a large majority of the work for you once you specify the columns you need from your models. (https://django-tables2.readthedocs.org/en/latest/)

Is this all of the functionality that you'll need? Have you considered looking at the built-in admin, which provides this functionality with little to no coding required?

-James

sum abiut

unread,
Jan 12, 2015, 6:25:06 PM1/12/15
to django...@googlegroups.com
Hi James,

I have already  coding the two views my problem is updating the rows.  I am a bit confuse on how to get that done.

Cheers,


For more options, visit https://groups.google.com/d/optout.



--

sum abiut

unread,
Jan 12, 2015, 6:58:40 PM1/12/15
to django...@googlegroups.com
Hi James,

Thanks heaps for your help. your help is very much appreciated . I manage to fix my issue. I wouldn't have done it without your help. :)

Cheers.
-

sum abiut

unread,
Jan 12, 2015, 7:49:25 PM1/12/15
to django...@googlegroups.com
I am having a slight issue. click on the edit to working fine but when i make change ans click save. i get this error


Page not found (404)

Request Method: POST
Request URL: http://10.0.x.x:8000/update_form//

i think something is not right on th update_from.html. i just can't seem to figure it out.

here are my two templates


displayleave.html

<table id="t01" >
<tr>

<th>Edit</th>
  <th>First Name</th>
  <th>Last Name</th>
  <th>Position</th>
  <th>Department</th>
  <th>Leave Type</th>
 
 
</tr>
{%for a in new_leave%}
<tr>

<td><a href ="/update_form/{{ a.id}}/">edit</a></td>
<td>{{a.first_name}}</td>
  <td>{{a.last_name}}</td>
  <td>{{a.position}}</td>
  <td>{{a.department}}</td>
<td>{{a.leave_type}}</td>




update_form.html



<form action ="/update_form/{{a.id}}/" method="post">{%csrf_token%}

James Schneider

unread,
Jan 12, 2015, 8:49:20 PM1/12/15
to django...@googlegroups.com

In your update_form.html, change the action to:

action=""

This will cause the form to POST to the same URL that generated the form, which should be what you want.

More specifically, you don't have access to {{a.id}} in update_form.html (since this is a separate request from the one that generated the form for your list view), so the form action is missing the ID of the object to update, hence the reason the URL listed in your error has a double slash at the end.

-James

sum abiut

unread,
Jan 12, 2015, 8:54:22 PM1/12/15
to django...@googlegroups.com
Thanks heaps James, It works perfectly . :)

Cheers,


Reply all
Reply to author
Forward
0 new messages