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

10
  • if You need to change rest of selectors content based on firs article selection then you need to use Ajax in template to return rendered context from DB. Commented Sep 8, 2020 at 13:12
  • @ZarakiKenpachi thanks for reply, Do you mean if I use Ajax I can display multiple 'for loop' in my page? Commented Sep 8, 2020 at 13:21
  • you can change context displayed in question section based on selected article. Commented Sep 8, 2020 at 13:24
  • @ZarakiKenpachi Ah understood, thank you If I need I should consider using that Commented Sep 8, 2020 at 13:48
  • 1
    @bdemirka I solved this problem refer this topic thank you! stackoverflow.com/questions/8000022/… Commented Sep 9, 2020 at 12:17

1 Answer 1

-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.

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

4 Comments

I already leave my seat ... I'll check tomorrow and I'm gonna tell you about the result! thank you!
it is not a syntax error, you can use it both way
@bedemirka. If what you say is correct is new for me. I just check the official django documentation docs.djangoproject.com/en/3.1/topics/templates/…. 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.
thank you, but It shows same error after fix it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.