tags:

views:

155

answers:

2

Hello

I am trying to set up links to a view that allows to edit objects, in previous view. Model:

class List(models.Model):
 user = models.ForeignKey(User)
 name = models.CharField(max_length=200)
 type = models.PositiveIntegerField(choices=TYPE_DICT)

 def __unicode__(self):
  return self.name

 @models.permalink
 def get_absolute_url(self):
  return ('EditList', None, {'list_id':self.id} )

View:

  lists = List.objects.filter(user=request.user)
  array = []
  for list in lists:
   ListDict = {'Name':list.name, 'type':types[str(list.type)], 'edit':list }
   array.append(ListDict)
  context = { 'array':array,}

Template:

 {% for dict in array %}
  <tr>
  {% for key,value in dict.items %}
   {% ifequal key 'edit' %}
    <td>{{ key }}</td><td><a href="{{ value.get_absolute_url }}">{{ value.name }}</a></td>
   {% else %}
    <td>{{ key }}:&nbsp;</td><td>{{ value }}</td>
   {% endifequal %}
  {% endfor %}
  </tr>
 {% endfor %}

and urls conf:

urlpatterns = patterns('tst.list.views',
 (r'^$', 'list'),
 (r'^edit/(?P<list_id>\d+)/$', 'EditList')

,

What this line with link gives me is http://localhost/list/ as url, not http://localhost/list/edit/%5Bobjectid%5D/

Can anyone please tell me what am i doing wrong?

Alan

A: 

Ok. I got it working. What i needed to do ,was to give name to this view. When i changed my urlconf to this:

url(r'^edit/(?P<list_id>\d+)/$', 'EditList', name = 'EditList'),

Then everything started working.

Zayatzz
A: 

If you had wanted to do it for an unnamed urlconf, you just needed to pass the whole import string:

@models.permalink
def get_absolute_url(self):
    return ('yourproject.yourapp.views.EditList', None, {'list_id':self.id} )

I also suggest you follow the PEP8 naming conventions for functions.

SmileyChris
Thanks. By mentioning pep8, you suggest that it should be edit_list not EditList?
Zayatzz
Yeah, requiring a good standard coding is one of my peeves :). The ones which stood out: ListDict -> list_dict, consistent standard spacing in your dicts and tuples, 4 spaced (or tabbed) indentation.
SmileyChris