Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

Python Django duplicate loop

i fetch two table from my db like this:

query_tags = Tags.objects.all()
query_usertags = UserNews.objects.all()

context = {'query_tags': query_tags, 'query_utags': query_usertags}

and in my html , i try that:

   {% for tags in query_tags %}
        {% for utags in query_utags %}

            {% if utags.user_tag == tags.name and utags.userid == user.id %}

            <input disabled type="checkbox" id="development" value={{ tags.name }} name="user_interest">
            <label class="light" for="development">{{ tags.name }}</label><br>
            {% else %}
                <input  type="checkbox" id="development" value={{ tags.name }} name="user_interest">
            <label class="light" for="development">{{ tags.name }}</label><br>

            {% endif %}
        {% endfor %}
      {% endfor %}

but my problem is output gonna duplicated cuz of second loop can you help me ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Find the associations between Tags and UserNews in your view and set up a structure that can be queried from your template. Sorry, it is hard to tell what the relationship is between those two objects based on the code you posted, either logical or in your Django models. Also worth examining why you don't have a formal relationship between your models if one exists and you want to query it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...