1

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/[objectid]/

Can anyone please tell me what am i doing wrong?

Alan

3
  • What is dict in the template? You're passing in a list confusingly called array in the context, but I can't see anything called dict. Commented Sep 9, 2009 at 10:42
  • Sorry. It seems i left out some of the code. dict is part of the array that gets put together in view - ListDict Commented Sep 9, 2009 at 11:14
  • and Value is List object Commented Sep 9, 2009 at 11:15

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. By mentioning pep8, you suggest that it should be edit_list not EditList?
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.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.