How can I automatically add the blogpost author into the member list?

chee

Everytime a blogpost is added, a memberlist will be generated. How do I automatically make it such that the author of the post will be included in the member list? I have added the create blog view

models.py

class BlogPost(models.Model):
 chief_title                    = models.CharField(max_length=50, null=False, blank=False, unique=True)
 body                   = models.TextField(max_length=5000, null=False, blank=False)
 members    = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="members")
 author                     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)


class Account(AbstractBaseUser):
 email                  = models.EmailField(verbose_name="email", max_length=60, unique=True)
 username               = models.CharField(max_length=30, unique=True)

views.py

from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin


class BlogPostMixin(object):
    model=BlogPost

class MemberListView(
    LoginRequiredMixin,
    UserPassesTestMixin,
    BlogPostMixin,
    DetailView
):
    login_url = 'must_authenticate'
    template_name = "HomeFeed/membersof_yourpost.html"

    def test_func(self):
        post = self.get_object()
        return self.request.user in post.members.all()


def create_blog_view(request):
    context = {}
    user = request.user
    if not user.is_authenticated:
        return redirect('must_authenticate')
    form = CreateBlogPostForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        obj= form.save(commit = False)
        author = Account.objects.filter(email=user.email).first()
        obj.author = author
        obj.save()
        context['success_message'] = "Updated"
        return redirect('HomeFeed:main')
    context['form'] = form
    return render(request, "HomeFeed/create_blog.html", {})


leung2
obj.save()
obj.members.add(request.user) #change is here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i add a list to a list in a dictionary

From Dev

How can I automatically add workspaces, only if I need them?

From Dev

How can I use schema.org to label a person (without repeating myself) as both a member of an organization and the author of a book?

From Dev

How can I get the name of comments' author?

From Dev

How can I generate a Composer author list from Git's commit history?

From Dev

How can I sort a list of objects by a member of each object?

From Dev

How can I add the compilation date to java project automatically

From Dev

How can I automatically add a class to a link based on active anchor?

From Dev

Android how can I get coordinates and add it to link automatically

From Dev

How can I add a description of the deprecated list?

From Dev

How can I add from a CheckBoxList to a List<>?

From Dev

How can I add delete button on list?

From Dev

How can I add new item in a list?

From Dev

How Can I add linked list to vector?

From Dev

How can I add bunch of strings to list

From Dev

How can I add a reference to a List

From Dev

How can I create a list of named folders in Windows automatically?

From Dev

How can I add nothing to the list in list comprehension?

From Dev

How can I add an item to a list and return a new list

From Dev

How can I list all modified files by an author between a commit range but only with the last thing that happened to the file in Git?

From Dev

How to add a new member to the list in a function in R

From Dev

How can I get the author of each post in the query?

From Dev

How can I extract the Author of the book using Jsoup?

From Dev

How can I author a Snippet Collection to multiple languages in VSCode Marketplace?

From Dev

How can I make a TextView automatically scroll as I add more lines of text?

From Dev

How can I automatically sum values that I add from a combo box to a text area?

From Dev

How can i get libreoffice writer to automatically add a closed bracket when I open a bracket?

From Dev

How can I add member functions in built-in classes in c++?

From Dev

How can remove all member of the list in python

Related Related

  1. 1

    How can i add a list to a list in a dictionary

  2. 2

    How can I automatically add workspaces, only if I need them?

  3. 3

    How can I use schema.org to label a person (without repeating myself) as both a member of an organization and the author of a book?

  4. 4

    How can I get the name of comments' author?

  5. 5

    How can I generate a Composer author list from Git's commit history?

  6. 6

    How can I sort a list of objects by a member of each object?

  7. 7

    How can I add the compilation date to java project automatically

  8. 8

    How can I automatically add a class to a link based on active anchor?

  9. 9

    Android how can I get coordinates and add it to link automatically

  10. 10

    How can I add a description of the deprecated list?

  11. 11

    How can I add from a CheckBoxList to a List<>?

  12. 12

    How can I add delete button on list?

  13. 13

    How can I add new item in a list?

  14. 14

    How Can I add linked list to vector?

  15. 15

    How can I add bunch of strings to list

  16. 16

    How can I add a reference to a List

  17. 17

    How can I create a list of named folders in Windows automatically?

  18. 18

    How can I add nothing to the list in list comprehension?

  19. 19

    How can I add an item to a list and return a new list

  20. 20

    How can I list all modified files by an author between a commit range but only with the last thing that happened to the file in Git?

  21. 21

    How to add a new member to the list in a function in R

  22. 22

    How can I get the author of each post in the query?

  23. 23

    How can I extract the Author of the book using Jsoup?

  24. 24

    How can I author a Snippet Collection to multiple languages in VSCode Marketplace?

  25. 25

    How can I make a TextView automatically scroll as I add more lines of text?

  26. 26

    How can I automatically sum values that I add from a combo box to a text area?

  27. 27

    How can i get libreoffice writer to automatically add a closed bracket when I open a bracket?

  28. 28

    How can I add member functions in built-in classes in c++?

  29. 29

    How can remove all member of the list in python

HotTag

Archive