Rico
Rico

Reputation: 6032

Passing DJango template variable as an input parameter

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">&nbsp;</label>
               <input type="submit" value="Remove  &#9658;"></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?

Upvotes: 2

Views: 1929

Answers (3)

Michael Sayapin
Michael Sayapin

Reputation: 163

Seems like a typo in devSkills = DevSkills(...) string. You're actually instantiating new object instead of getting records from the database. You need to use devSkills = DevSkills.objects.filter(...) to get the queryset, and then delete it.

PS: I would also suggest using django-debug-toolbar and Python native logging module for debugging purposes.

Upvotes: 1

Rico
Rico

Reputation: 6032

It turns out it was just too early in the morning.

Notice the silly mistake in the view:

devSkills = DevSkills(dev = userProfile, skill = request.POST.get('skill')

Which, clearly, needed to be:

devSkills = DevSkills.objects.get(dev = userProfile, skill = request.POST.get('skill'))

I need to get an object to delete - not create one!

Problem solved.

Upvotes: 0

Williams
Williams

Reputation: 4318

Is the following what you're looking for (parse based on 'name' parameter in view)?

   <li> {{ skill.skill }} 
        <input type="hidden" name="skill_{{ skill.pk }}" value={{ skill.skill }}> 
        <label class="formlabel">&nbsp;</label>
        <input type="submit" value="Remove  &#9658;"></li>

In the for loop over request.POST you can easily parse using .split("_")[-1].

This is none too elegant. Using formsets (https://docs.djangoproject.com/en/dev/topics/forms/formsets/) is actually probably what you're looking for.

Upvotes: 0

Related Questions