I’m using Django 1.4 with Python 2.7 on Ubuntu 12.04.
I have a template that uses Django’s template syntax to generate a list of objects. I want to create a “form post” on each item to give the option to delete that item from the list. Pretty simple:
MySQL <remove>
Linux <remove>
Java <remove>
Python <remove>
C++ <remove>
PostgreSQL <remove>
Django <remove>
Where <remove> is a “submit”.
Here is what the template looks like:
{% if not dev.user.is_superuser %}
{% if dev.user.is_authenticated and dev.user.is_staff %}
<ul>
{% for skill in dev.skill_set.all %}
<form action="/removeSkill/" method="post">{% csrf_token %}
<li>
{{ skill.skill }}
<input type="hidden" name="skill" value={{ skill.skill }}>
<label class="formlabel"> </label>
<input type="submit" value="Remove ►"></li>
</form>
{% endfor %}
</ul>
{% endif %}
I need to know how to pass the {{ skill.skill }} value as the hidden input’s value.
Any suggestions?
UPDATE 1:
Ok, it looks like what I’m doing in the template is working wonderfully. I just can’t seem to actually delete the entry from the database in the view.
def remove_skill(request):
"""
.. function:: remove_skill()
Remove a skill for a developer
:param request: Django Request object
"""
## Create a logging object
path = os.path.join(os.path.dirname(__file__), 'logs/')
filename = '{0}debug.log'.format(path)
logfile = open(filename, 'w')
now = datetime.datetime.now()
logfile.write('\n --------------------- {0}\n'.format(now))
if (request.user.is_authenticated() and request.user.is_staff):
userProfile = UserProfile.objects.get(user_id = request.user.id)
devSkills = DevSkills(dev = userProfile, skill = request.POST.get('skill'))
logfile.write('user = {0}\n'.format(devSkills.dev_id))
logfile.write('skill to remove = {0}\n'.format(devSkills.skill))
devSkills.delete()
logfile.close()
return dev_profile(request)
The debug.log file shows information for the correct entry, but I continue to get the following error when I try and remove a skill:
DevSkills object can't be deleted because its id attribute is set to None.
Suggestions?
It turns out it was just too early in the morning.
Notice the silly mistake in the view:
Which, clearly, needed to be:
I need to get an object to delete – not create one!
Problem solved.