0

Hey guys I'm making English test system for education by using Django python.
the system display one article and several(not fixed) questions about the article.
and every question has selectable items which numbers are not fixed.

for example
{ ---------- article -------------- }
{ ---------- question --------------}
{ ---------- item ------------------}
{ ---------- item ------------------}
{ ---------- item ------------------}
{ ---------- question --------------}
{ ---------- item ------------------}
{ ---------- item ------------------}
{ ---------- item ------------------}

so I defined model like this :

# Question DB
class Article(models.Model):
    a_name = models.TextField(unique=True)
    a_con = models.TextField() #Article Contents

class Question(models.Model):
    q_name = models.TextField(unique=True)
    q_a = models.ForeignKey(Article,on_delete=models.CASCADE)
    q_con = models.TextField() #Question Contents
    q_ans = models.TextField() #Question Correct Answer
    
class Item(models.Model):
    i_q = models.ForeignKey(Question,on_delete=models.CASCADE)
    i_seq = models.PositiveIntegerField() #Item Sequence
    i_con = models.TextField() #Item Contents

# Image DB
class ArticleImage(models.Model):
    ai_name = models.TextField(unique=True)
    ai_a = models.ForeignKey(Article,on_delete=models.CASCADE)
    ai_src = models.TextField()

class QuestionImage(models.Model):
    qi_name = models.TextField(unique=True)
    qi_q = models.ForeignKey(Question,on_delete=models.CASCADE)
    qi_src = models.TextField()

class ItemImage(models.Model):
    ii_name = models.TextField(unique=True)
    ii_i = models.ForeignKey(Item,on_delete=models.CASCADE)
    ii_src = models.TextField()

and I wrote view.py like this:

def findQuestionbyArticle(article_name):
    _ARTICLE_ = Article.objects.filter(a_name = article_name)
    for a in _ARTICLE_:
        _ARTICLE_IMAGE_ = ArticleImage.objects.filter(ai_a = a)
        _QUESTION_ = Question.objects.filter(q_a = a)
        _ITEM_ = {}
        _ITEM_IMAGE_ = {}
        for q in _QUESTION_:
            _QUESTION_IMAGE_ = QuestionImage.objects.filter(qi_q = q)
            _ITEM_[q] = Item.objects.filter(i_q = q)
            for i in _ITEM_[q]:
                _ITEM_IMAGE_[i] = ItemImage.objects.filter(ii_i = i)
    return {
        'article' : _ARTICLE_, 
        'article_image' : _ARTICLE_IMAGE_ , 
        'question' : _QUESTION_,
        'question_image' : _QUESTION_IMAGE_,
        'item' : _ITEM_,
        'item_image' : _ITEM_IMAGE_
    }

and I wrote index.html for template like this:

<div class = "content-a">
        <div class = "article">
            {%for article in articles%}
                <span>{{article.a_con}}</span>
                {%for article_image in article_images%}
                    <img src = {{article_image.src}}>
                {%endfor%}
            {%endfor%}
        </div>
        {%for question in questions %}
            <div class = "question">
                <span>{{question.q_con}}</span>
                {%for question_image in question_images %}
                    <img src = {{question_image.src}}>
                {%endfor%}
                {%for item in items[question] %}
                <div class = "item">
                    <span>{{item.i_con}}</span>
                    {%for item_image in item_images%}
                        <img src = {{item_image[item].src}}>
                    {%endfor%}
                </div>
                {%endfor%}
            </div>
        {%endfor%}
    </div>

Of course, It doesn't work ... In this case, how can I make it work ?
If you have any idea please help me ... this problem is bothering me during a week ...

EDIT:

the result shows this error page.

enter image description here

Lukas
  • 64
  • 5

1 Answers1

-1

To begin with, you may have a look at your syntax. You're making a repeated mistake at the functions calls such as:

{%for article in article%} instead of {% for article in article %}
{%for question_image in question_images %} instead of  {% for  
{%endfor%} instead of {% endfor %}

Correct them and try again. It might work. For such a case, you may have a look at the correct tag format in the official django documentation.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
Erick
  • 301
  • 3
  • 12
  • I already leave my seat ... I'll check tomorrow and I'm gonna tell you about the result! thank you! – Lukas Sep 08 '20 at 13:51
  • it is not a syntax error, you can use it both way – bdemirka Sep 08 '20 at 14:32
  • @bedemirka. If what you say is correct is new for me. I just check the official django documentation https://docs.djangoproject.com/en/3.1/topics/templates/#the-django-template-language. You will see that what I'm proposing is the correct syntax, and in my experience I messed up whenever I format the tags as you 're proposing. – Erick Sep 08 '20 at 16:30
  • thank you, but It shows same error after fix it. – Lukas Sep 09 '20 at 09:43