Note that there are some explanatory texts on larger screens.

plurals
  1. PO(re)Using dictionaries in django views
    primarykey
    data
    text
    <p>I have this dictionary in my apps model file:</p> <pre><code>TYPE_DICT = ( ("1", "Shopping list"), ("2", "Gift Wishlist"), ("3", "test list type"), ) </code></pre> <p>model, which uses this dict is this:</p> <pre><code>class List(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=200) type = models.PositiveIntegerField(choices=TYPE_DICT) </code></pre> <p>I want to re-use it in my views and imported it from apps.models. I am creating a list of dictioneries to use in my view like this:</p> <pre><code>bunchofdicts = List.objects.filter(user=request.user) array = [] for dict in bunchofdicts: ListDict = {'Name':dict.name, 'type':TYPE_DICT[dict.type], 'edit':'placeholder' } array.append(ListDict) </code></pre> <p>When i use this list in my template then it gives me very strange results. Instead of returning me the type of the list (shopping list) it returns me ('2', 'Gift Wishlist').</p> <p>So i can understand what it is doing (in ths case, the dict.type equals 1, and it should return me "shopping list" , but it returns me [1] - second, element in list). What i do not understand, why doing exactly the same thing in python shell gives different results.</p> <p>doing it the way i do in django ( TYPE_DICT[dict.type] ), works as described above and creates error in python shell. using TYPE_DICT[str(dict.type)] in python shell works just fine, but creates this error in django:</p> <pre><code>TypeError at /list/ tuple indices must be integers, not str Request Method: GET Request URL: http://127.0.0.1/list/ Exception Type: TypeError Exception Value: tuple indices must be integers, not str Exception Location: /home/projects/tst/list/views.py in list, line 22 Python Executable: /usr/bin/python Python Version: 2.6.2 </code></pre> <p>Perhaps i did something wrong or different in python shell. What i did was:</p> <pre><code>python Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; dict = {'1':'shoppinglist', '2':'giftlist','3':'testlist'} &gt;&gt;&gt; print dict[1] Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; KeyError: 1 &gt;&gt;&gt; print dict[str(1)] shoppinglist &gt;&gt;&gt; x = 1 &gt;&gt;&gt; print dict[x] Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; KeyError: 1 &gt;&gt;&gt; print dict[str(x)] shoppinglist &gt;&gt;&gt; </code></pre> <p>so what is wrong here?</p> <p>Alan</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload