How to create groups for users in Django

1,317 views
Skip to first unread message

dszcz...@poczta.pl

unread,
Mar 3, 2016, 12:47:56 PM3/3/16
to Django users

Hello! 

I have a problem, like in topic, with creating groups in django. I was searching everywhere and nothing was so interested.

What i want from this section: I would like to by filling a column with the name of the group, formed a quite new group, on the occasion of adding me as one of the members and preferably, as a leader, who can add other users to the group.

My problem: I did one form for name, and when i sending a request, i got nothing, only the stie is re-loaded. I have some code but don;t know what to do next..


My code:


models.py:


class Person(models.Model):

    name = models.CharField(max_length=128)

def __str__(self):              # __unicode__ on Python 2
    return self.name


class Group(models.Model):
    name = models.CharField(max_length=128)
    leader = models.CharField(max_length=50)
    members = models.ManyToManyField(Person, through='Membership')

    def __str__(self):              # __unicode__ on Python 2
        return self.name


class Membership(models.Model):
    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)



forms.py:



class GroupForm(forms.ModelForm):

class Meta:
    model = Group
    fields = ('name',)



views.py:


@login_required
def groups(request):

if request.method == "POST":
    form = GroupForm(request.POST)
    if form.is_valid():

        grupa = Group.objects.create(name="grupa")
        per = Person.objects.create(name=request.user)
        m1 = Membership(person=per, group=grupa, date_joined=(1999, 8, 8), invite_reason="Needed programmer.")
        form.save()


        return render(request, 'groups.html', {'form':form})

else:
    form = GroupForm()

return render(request, 'groups.html', {'form': form})



groups.html:


{% block profile %}

 <br>
  <div class="jumbotron">
    <h4>Create your new group !</h4>
    <form method="POST" class="post-form">
        {% csrf_token %}
        {{ form|crispy }}
        <button type="submit" class="save btn btn-default">Go!</button>
    </form>
  </div>

{% endblock %}



This is it... Can anyone could help me with that? I spend few days with it and 0 progress.


Thanks for any help!

Damian

Luis Zárate

unread,
Mar 4, 2016, 8:05:14 PM3/4/16
to django...@googlegroups.com
Años observation

Person.objects.create(name=request.user)

Lazy object is set but name is charfield.
M1is not saved.
form.save() create other group that is not used.
> --
> 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 post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b71b3e7e-a168-4f1c-aaf7-655bdee86212%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Reply all
Reply to author
Forward
0 new messages