Giter Club home page Giter Club logo

social-network's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

social-network's Issues

Separate the logic of creating new post and creating new comment into two different functions.

Hello @hellopyplane . I was following this project and I have come up with an enhancement.

The code for creating a new post and creating a new comment is written in the function post_comment_create_and_list_view() in posts:views.py.

Currently existing code and logic for creating new post

if 'submit_p_form' in request.POST:
        print(request.POST)
        p_form = PostModelForm(request.POST, request.FILES)
        if p_form.is_valid():
            instance = p_form.save(commit=False)
            instance.author = profile
            instance.save()
            p_form = PostModelForm()
            post_added = True

Currently existing code and logic for creating new comment

if 'submit_c_form' in request.POST:
        c_form = CommentModelForm(request.POST)
        if c_form.is_valid():
            instance = c_form.save(commit=False)
            instance.user = profile
            instance.post = Post.objects.get(id=request.POST.get('post_id'))
            instance.save()
            c_form = CommentModelForm()

I wanted to ask if we can separate the logic into two different functions as follows.

Proposed code for creating new post

def create_post(request):

    if request.method == 'POST':
        logged_in_user_profile = Profile.objects.get(user=request.user)

        # Handle Post form submission.
        new_post_form = PostModelForm(request.POST, request.FILES)
        if new_post_form.is_valid():
            instance = new_post_form.save(commit=False)
            instance.author = logged_in_user_profile
            instance.save()

    return redirect('posts:main-post-view')

Proposed code for creating new comment

def submit_new_comment(request):

    if request.method == 'POST':
        logged_in_user_profile = Profile.objects.get(user=request.user)

        # Handle Comment form submission.
        new_comment_form = CommentModelForm(request.POST)
        if new_comment_form.is_valid():
            instance = new_comment_form.save(commit=False)
            instance.user = logged_in_user_profile
            instance.post = Post.objects.get(id=request.POST.get('post_id'))
            instance.save()

    return redirect('main-post-view')

Then we can set the urls to the respective functions and call them in the html form instead of specifying different names to the buttons.

urls.py

...
urlpatterns = [
    ...
    path('create-post', views.create_post, name='create-post'),
    path('submit-new-comment', views.submit_new_comment, name='submit-new-comment'),
    ...
]

HTML file

...
<form action="{% url 'create-post' %}" method="post" enctype="multipart/form-data">
...
<form action="{% url 'submit-new-comment' %}" method="post">
...

I am new to contributing to open source so please guide me if I am wrong. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.