Sunday, May 17, 2020

Re: How to create custom tag - break for tamplate?

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 написал:
Hi
I would like abort a loop in my template.
How to create custom tag - break for tamplate?

--
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+unsubscribe@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.

No comments:

Post a Comment