0

here is the output from $python manage.py shell

>>> a=Mocument.objects.all()
>>> a
[<Mocument: abc.xlsx>, <Mocument: 1.csv>, <Mocument: ok.csv>, <Mocument: 11.csv>, <Mocument: 12.csv>]
>>> a[0]
<Mocument: abc.xlsx>
>>> for i in a:
...     print i
... 

here is the output

abc.xlsx
1.csv
ok.csv
11.csv
12.csv

till here all great. problem starts when i try to retrieve data in HTML template. here is my html file code

<html>
    <head>
        <meta charset="utf-8">
        <title>Minimal Django File Upload Example</title>   
    </head>

    <body>
        <!-- List of uploaded documents -->
        {% a=Mocument.objects.all() %}      
        {% for i in a %}
            <p>{% print i %}</p>    
        {% endfor %}

    </body>         
</html> 

here is the Error details

Error:-------------------- Exception Value: Invalid block tag: 'a=Mocument.objects.all()'

please help.

1
  • You cannot assign variables in the template like that. Define "a" in the template context in a view. Commented Sep 23, 2013 at 0:27

1 Answer 1

2

You are not writing valid django template code. You cannot use any python code in templates. You have to use specific django tags and filters. Have a read here: https://docs.djangoproject.com/en/1.5/topics/templates/

I your case, you should do this:

{% for i in mocument_objects %}
    <p>{{ i }}</p>    
{% endfor %}

You would need to pass monument_objects into your template context from the view.

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

4 Comments

you right, i did the same as it is in this post stackoverflow.com/questions/5871730/…. its doing everything except its not printing the file names. even the BULLETS are printed but filename is missing/blank
Did you override the __unicode__ method on the Mocument model?
there are two class in model.py and i have added def __unicode__(self): return self.mocfile . is it ok?
@ashirnasir maybe you are using {% print i %} instead of {{ i }}. That's probably why you are not getting the filenames. I suggest you read the django docs about The Django template language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.