pyplane / social-network Goto Github PK
View Code? Open in Web Editor NEWLearn Django on a project based tutorial available for free on youtube
Learn Django on a project based tutorial available for free on youtube
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
.
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
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.
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')
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.
...
urlpatterns = [
...
path('create-post', views.create_post, name='create-post'),
path('submit-new-comment', views.submit_new_comment, name='submit-new-comment'),
...
]
...
<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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.