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.
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()inposts:views.py.Currently existing code and logic for creating new post
Currently existing code and logic for creating new comment
I wanted to ask if we can separate the logic into two different functions as follows.
Proposed code for creating new post
Proposed code for creating new comment
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
HTML file
I am new to contributing to open source so please guide me if I am wrong. Thank you.