How to create custom tag - break for tamplate?

8 views
Skip to first unread message

Sergei Sokov

unread,
May 16, 2020, 2:05:01 PM5/16/20
to Django users
Hi
I would like abort a loop in my template.
How to create custom tag - break for tamplate?

Sergei Sokov

unread,
May 17, 2020, 11:10:23 AM5/17/20
to Django users

views.py

class MyClassCreateView(LoginRequiredMixin, CustomSuccessMessageMixin, CreateView):
model
= MyClass
template_name
= 'mypage.html'
form_class
= MyClassForm
success_url
= reverse_lazy('mypage')
success_msg
= 'сохранён'
def get_context_data(self, **kwargs):
    kwargs
['list_mylist'] = MyClass.objects.all()
    list_mylist
= kwargs['list_mylist']
   
for i in list_mylist:
       
if i.author == self.request.user:
           
print('my_button')
            kwargs
['my_button'] = True
           
break
   
return super().get_context_data(**kwargs)

template

        {% if my_button %}
       
{% for i in list_mylist %}
             
{% if request.user == i.author %}
               
<a class="btn btn-primary" href="#" role="button">
                 
кнопка первая
               
</a>
             {% endif %}
       {% endfor %}
{% else %}
    <button type="button" class="btn btn-primary mr-4" data-toggle="modal" data-target="#class">
          кнопка вторая
        </
button><br>
   
{% endif %}



суббота, 16 мая 2020 г., 20:05:01 UTC+2 пользователь Sergei Sokov написал:

Gabriel Araya Garcia

unread,
May 17, 2020, 8:38:49 PM5/17/20
to django...@googlegroups.com
Sergei, you just to have put an conditional flag. It could be defined in your view. Example:
flag_ = 0
if pedrito > 16  
   flag_ = 1

and flag_ send it in context 

in template you must include:
{%  if flag_ == 0 %}
      brek
      etc.

Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos
99.7721.15.70



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5f05fc6e-13a0-47ba-be33-9c860dc48dee%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages