I want the sign up button to disappear when I sign up (in django templates)'

Model

class TeamMember(models.Model):
    team = models.ForeignKey(TeamInfo, on_delete=models.CASCADE)
    member = models.ForeignKey(User, on_delete=models.CASCADE)
    position = models.CharField(max_length=50,default="member")

class TeamInfo(models.Model):
    leader = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False)
    team_name = models.CharField(max_length=50, unique=True)
    team_description = models.TextField(blank=True)
    member_count = models.IntegerField(default=1)

    def __str__(self):
        return self.team_name

View

class TeamInfoListView(LoginRequiredMixin,ListView):
    model = TeamInfo
    paginate_by = 20

template (https://github.com/hyunsokstar/django_inflearn2/blob/master/todo/templates/todo/teaminfo_list.html)

{% for tm in p.teammember_set.all %}
    {% if tm.member.username == request.user.username %}
        <a class="btn btn-outline-info btn-sm team_register_button" data-id = {{p.id}} data-user={{request.user.id}} >탈퇴</a>
    {% endif %}
{% endfor %}

I have two questions.

1.How can I make the sign up button disappear when a user is registered? 2.For logic seems to be inefficient at present~! Is there a way to improve it?

Thank you for letting me know about this.

=======================================================================

As you said, I created a logic using get_context_data and filter() in views.py so that solved the problem. Thank you

problem is sloved by this

class TeamInfoListView(LoginRequiredMixin,ListView):
    model = TeamInfo
    paginate_by = 20

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(type(self), self).get_context_data(**kwargs)
        myteam=TeamMember.objects.filter(member=self.request.user)

        if myteam.exists():
            myteam=TeamMember.objects.get(member=self.request.user).team
            print("myteam : ", myteam)

        context['myteam']= myteam

        return context
<td>
    {% if p.team_name == myteam.team_name %}
        <a class="btn btn-outline-info btn-sm team_register_button" data-id = {{p.id}} data-user={{request.user.id}} >탈퇴</a>
    {% else %}
        <a class="btn btn-outline-info btn-sm team_register_button" data-id = {{p.id}} data-user={{request.user.id}} >가입</a>
    {% endif %}
</td>


What about passing a variable from your view?

View Code

def your_view(request):
    member_of_teams = TeamMember.objects.filter(member=request.user)
    context = {'member_of_teams':member_of_teams}
    return render("yourtemplate.html", request, context)

You can then use your for loop to loop through fewer elements.

As for how you hide the button, what you already have with the

{% if tm.member.username == request.user.username %}

should work. You just need to change it to see if the team matches with the elements in member_of_teams, not the username.

Django: How to make buttons disappear/disabled after pushed , Using Javascript (and specially library like jquery) will make this thing easier and intuitive. And also I would not use GET for this, I would use  This is a standard Django form using POST to send data and {% csrf_token %} tags for security concerns, namely to prevent a CSRF Attack. The form's contents are outputted between paragraph tags thanks to {{ form.as_p }} and then we add a "submit" button. Next update the settings.py file to tell Django to look for a templates folder at the


You can do 2 things here:

  1. Override your get_context_data to return teams that the logged in user is a member of:
def get_context_data(self, *args, **kwargs):
    context = super().get_context_data(*args, **kwargs)
    context['logged_in_user_teams'] = TeamInfo.objects.filter(team_member__user=request.user)
    return context

Then simply check in your template if the team you're displaying is in that list:

{% for team in object_list %}
    {% if not team in logged_in_user_teams %}
        <a>Subscribe</a>
    {% endif %}
{% endfor %}

Also, note that we're iterating over object_list since that is what your ListView sets on context.

  1. Keep things the same (but fix object_list in template) and write a small template filter:
{% for team in object_list %}
    {% if not team|contains:request.user %}
        <a>Subscribe</a>
    {% endif %}
{% endfor %}

Your template filter should look something like this:

def contains(team, user):
    return team.teammember_set.filter(member=user).exists()

I have a navbar in which i want to display login & signup button , I have a navbar in which i want to display login & signup button while the user is in main when he clicks the button it takes him to the respective page and the buttons disappear. Django Rest Framework Starter Template. I have modified the login.tpl to remove the register button and I have also disabled the register page. I am happy for the staff menu to appear on the main menu when a staff member is logged in, but I do not want the Login and Sign Up options to be displayed on the main menu when no one is logged in.


Probably the most optmized way is to use annotation like this:

First override the get_queryset() method:

from django.db.models import Case, When, Value, BooleanField

 class TeamInfoListView(LoginRequiredMixin,ListView):
     model = TeamInfo
     paginate_by = 20

    def get_queryset(self):
       queryset = super(TeamInfoListView, self).get_queryset()
       queryset = queryset.annotate(subscribed = Case(
           When(teammember__member=self.request.user, then=Value(True)),     
           default=Value(False),
           output_field=BooleanField(),)
       )
       return queryset

Then in template:

{% for team in object_list %}
    {% if not team.subscribed %}
        <a class="btn btn-outline-info btn-sm team_register_button" data-id = {{p.id}} data-user={{request.user.id}} >가입</a>
    {% endif %}
{% endfor %}

This way is most optimized because you are calculating if the user is subscribed or not in DB level, also it will be executed once.And you don't have to write any if-else logic in forloop inside the template.

Django Template: remove the buttons when session starts, Django Template: remove the buttons when session starts - html. I have made sign in and sign up buttons in the upper navbar. Now What I need to achieve is when I sign in to the application the redirection would take place and then the sign in and sign up button disappears and the User ABC button comes at its place. from django.shortcuts import render from django.contrib.auth.models import User from django.contrib.auth import authenticate, login from django.http import HttpResponseRedirect from django import forms from .forms import UserRegistrationForm # Create your views here.


You can set an empty else in django template to render nothing.

{% for tm in p.teammember_set.all %}
{% if tm.member.username == request.user.username %}
    <a class="btn btn-outline-info btn-sm team_register_button" data-id = 
{{p.id}} data-user={{request.user.id}} >탈퇴</a>
{% else %}
    <a class="btn btn-outline-info btn-sm team_register_button">Subscribe</a>
{% endif %}
{% endfor %}

Implementing and customising Lazy Signup in Django, Templates. In my parent template, I can detect whether the user is a lazy user and provide a Sign up or Login button if they are, or  from django.contrib.auth import login from django.contrib.auth.models import User from django.shortcuts import render, redirect from django.utils.encoding import force_text from django.utils.http import urlsafe_base64_decode from mysite.core.tokens import account_activation_token def activate (request, uidb64, token): try: uid = force_text


The missing django-allauth tutorial, I don't remember why I ultimately settled on using django-allauth for my task, Of course things rarely actually disappear from the internet, so I soon 10 Style the login and registration templates; 11 Customize the email message; 12 Fin (You may first have to sign up as a Facebook Developer before the  So always use a custom user model for all new Django projects. However the official documentation example is not actually what many Django experts recommend using. There is a far easier yet still powerful approach to starting off new Django projects with a custom user model which I'll demonstrate here.


Hide 'Login' button when user authorized · Issue #18 · mbr/flask-nav , GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Sign up. Django Project Boilerplate. This repository is a boilerplate Django project for quickly getting started. Getting started. Steps: Clone/pull/download this repository; Create a virtualenv with virtualenv env and install dependencies with pip install -r requirements.txt; Configure your .env variables


User register form, Please complete all sections and attach this template to an email addressed to IT. I will cover a few strategies to create Django user sign up/registration. vic. Scroll down to 'User Registration Addon' and click on the Install Addon button next Tick this if you want to hide this value Create a form to ask for the registration  If you encounter errors or missing functionality in setting up this template, please submit an issue to the issue tracker, or email django.danceschool@gmail.com. Introduction The Django Dance School project is designed to be exceptionally flexible and modular, and it can be run locally as well as on any server system that supports Django.