Create A Group Using Form


I want to create a Group using a form. And i’am kind a lost on how to accomplish this true a easy way.
Hope someone can pinpoint me in de wright direction.


Roel Knippen

Create a CreateView for the Group model. It really is that easy.

Hi Ken,

Just to check if I’am on de write track :slight_smile: I’am new to Django and Python so I’am learning :slight_smile:

I updated the whit the following line:
‘’‘,Group.add_to_class(‘Omschrijving’, models.CharField(max_length=100,null=True, blank=True))’‘’.
this is because I need a description on the Group Name

Then I added to the following code:
class RoleForm(forms.Form):
name = forms.CharField(max_length=100, required=True)
omschrijving = forms.CharField(max_lenghth=500, widget=forms.Textarea)

In the I added the following code:
‘’'class RoleFormView(CreateView):
template_name = ‘leden/role.html’
form_class = RoleForm

def form_valid(self, form):
    return super().form_valid(form)'''

And I created a role.html. where I put in the following code:

{% crsf_token %} {{ form.as_p }} ''' When I start the server I don't get any error. But I think i need to make a change to the inside my app make te role.html visisble. And the conform the link you provided I can build the createview Updateview and deleteview?

Hopfully I’am working in the wright direction. Thanks for the feedback

Greeings Roel

First a side note - when you’re trying to mark off code, you need to use three backticks - `, not apostrophes - '.

This isn’t a good thing to do. You really don’t want to do this.

If you need to extend the system-provided Group class, create a “profile” class similar to what you might do with User. (A “profils” class is a model with a one-to-one relationship with a base class. See Customizing authentication in Django | Django documentation | Django for a description of a “profile” class as it would be for User.)

Or, depending upon how you’re planning to use these groups, you might want to ignore the system-provided Group model entirely and just create your own. (It all depends upon whether you’re using this for managing permissions or for some other purpose.)

Everything else looks like you’re headed in generally the right direction.

Hi Ken,

Thank for the Advice.

I did the following:
In my I added the following code:

class Functie(models.Model):
    Group = models.OneToOneField(Group, on_delete=models.CASCADE)
    Omschrijving = models.CharField(max_length=100)

    def __str__(self):
        return self.Omschrijving

In my I created a the following class:

class Functie(View):
    form_class = FunctieForm
    initial = {'key': 'value'}
    template_name = 'leden/Functie.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        return render(request, self.template_name, {'form': form})
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)

        if form.is_valid():
        return render(request, self.template_name, {'form': form})

Created a Functie.html whit the following content.

{% extends "leden/base.html" %}
{% block title %} CV-Pierreslikkers | Maak nieuwe Functie aan {% endblock title%}
{% block content %}
    <div class="form-content my-3 p-3">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-lg-7">
                    <div class="card shadow border-0 rounded-lg mt-0 mb-5">
                        <div class="card-header justify-content-center">
                            <h3 class="font-weight-light my-4 text-center">CV-Pierreslikkers | Nieuwe Functie</h3>
                    <div class="card-body">
                        <form method="POST">
                            {% csrf_token %}
                            <div class="form-row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="small mb-1">Functie Naam:</label>
                                        {{ form.Name }}
                            <div class="form-row">
                                <div class="col md-6">
                                    <div class="form-group">
                                        <label class="small mb-1">Omschrijving</label>
                                        {{ form.Omschrijving }}
                            <div class="form-group mt-4 mb-0">
                                <button type="submit" class="col-md-12 btn btn-dark">Maak een Functie aan</button>
                    <div class="card-footer text-center">
                        <div class="small">
                            <p>nog wat tekst neer zetten</p>
{% endblock %}

When I go to the Admin site I see the Functie table. But when I use the functie.html. to create a new group there isn’t anything happening. So I’am doing something wrong but i cannot find out what. Hope you can help me :slight_smile:

Greetings Roel Knippen

Please edit your post to put the three backticks - ``` before and after the code or template for each file.

# The line above this is ```
def function():
# The line after this is ```

Did it. Had the wrong backtick Sorry

Please also post your FunctieForm.

But even without seeing it yet, there are a couple things I can point out.

Since you’re working with two different objects (Functie and Group), the process is going to involve a little more work.

You’re allowing people to enter the name of the group. What do you want to have happen if they enter a name for a group that already exists? (What you decide is going to affect what validation you do on the form.)

  1. You’ve got this:

But you don’t have any field named key - you don’t need it.

  1. Likewise, since the intent here is to create a new instance of Functie, you don’t want to use the initial parameter in the call for form_class.

  2. Finally, my original suggestion was that you use a CreateView. Your CBV can be shortened even further using it instead of the more generic View. (Or even FormView.)

I know that understanding the flow of events through all the functions in a CBV can be confusing at first. That’s why I suggest people refer to the Classy Class-Based Views site along with the CBV diagrams page. They’re both very useful with helping to understand how CBVs “work”.

In addition to those references, you will want to become familiar with the docs at Generic editing views | Django documentation | Django as well. Keep in mind that the examples on that page are generally written to highlight one specific feature or usage of a CBV and are not a limitation on everything that can be done. (They’re a starting point, not an ending point.)

Thank’s for your time and patience

The thing i want t accomplish is:

I have a User. The user is connected to function and whitin the function the user has a role.
When logging in on the website the user gets to see his Functions + his role and the information or options that are connected to the function or role will be shown.

But this is nice learning expiernce to accomplisch this :slight_smile: