So whenever I create a new user I would like to select a group (executive or employee).
I made these two groups in the Admin environment. Within the admin environment I can assign groups to users, how do I do this from a form (register.html)?
Do I have to query the groups in my forms.py? Or in models.py? I wonder how to do this.
CodePudding user response:
You can do like this:
user.groups.add(Group.objects.get(name='executive'))
CodePudding user response:
Depending of the way you are using to render the html forms, you just need to pass the groups into the context so that you could use on the templates.
Example:
from django.contrib.auth.models import Group
def get_context_data(self, **kwargs):
context = super(NameOfYourView, self).get_context_data(**kwargs)
context['groups'] = Group.objects.all()
return context
In your template could try something like this:
{% block content %}
<h2>Publishers</h2>
<ul>
{% for group in groups %}
<li>{{ group.name }}</li>
{% endfor %}
</ul>
{% endblock %}
