def list_post(request): qs=Post.objects.all() profile=Profile.objects.get(user=request.user) post_form=PostModelForm() comment_form=CommentModelForm() profile=Profile.objects.get(user=request.user) post_added=False if "submit_p_form" in request.POST: post_form=PostModelForm(request.POST,request.FILES) if post_form.is_valid(): instance=post_form.save(commit=False) instance.author=profile instance.save() post_form=PostModelForm() post_added=True if "submit_c_form" in request.POST: comment_form=CommentModelForm(request.POST) if comment_form.is_valid(): instance=comment_form.save(commit=False) instance.user=profile instance.post=Post.objects.get(id=request.POST.get('post_id')) instance.save() comment_form=CommentModelForm() post_added=True context={ 'qs':qs, 'profile':profile, 'post_form':post_form, 'comment_form':comment_form, 'post_added':post_added, } return render(request,'posts/main.html',context)
def post_update(request, id): post = get_object_or_404(Post, id=id) form = PostModelForm(instance=post) if request.method == 'POST': form = PostModelForm(request.POST, request.FILES, instance=post) if form.is_valid(): form.save() return redirect('home') return render(request, 'posts/post_create.html', { 'form': form, 'action_url': 'post_update' })
def post_update_view(request,slug): unique_post = get_object_or_404(Post,slug = slug,) form = PostModelForm(request.POST or None, request.FILES or None, instance = unique_post) context = { 'form' : form } if form.is_valid(): form.save() messages.info(request, 'Successfully updated your blog post.') return redirect('/') return render(request,"posts/post_create.html",context)
def my_profile_view(request): profile = Profile.objects.get(user=request.user) form = ProfileModelForm(request.POST or None, request.FILES or None, instance=profile) confirm = False if request.method == 'POST': if form.is_valid(): form.save() confirm = True qs = Post.objects.filter(author=profile) # initials p_form = PostModelForm() c_form = CommentModelForm() post_added = False 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() context = { 'qs': qs, 'profile': profile, 'p_form': p_form, 'c_form': c_form, 'post_added': post_added, 'form': form, 'confirm': confirm, } return render(request, 'profiles/myprofile.html', context)
def post_create(request): if request.method == 'POST': form = PostModelForm(request.POST, request.FILES) if form.is_valid(): post = form.save(commit=False) post.author = request.user post.save() return redirect('posts:post-list') else: form = PostModelForm() context = {'form': form} return render(request, 'posts/post_create.html', context)
def post_create_view(request): author,created = Author.objects.get_or_create( user = request.user, email = request.user.email, phone_no = 9896165054 ) form = PostModelForm(request.POST or None, request.FILES or None ) context = { 'form' : form } if form.is_valid(): form.instance.author = author form.save() return redirect('/') messages.info(request, 'Successfully created a new blog post!') return render(request,"posts/post_create.html",context)
def post_create(request): form = PostModelForm() # form = PostForm() if request.method == 'POST': # form = PostForm(request.POST) form = PostModelForm(request.POST, request.FILES) if form.is_valid(): print("Valid data") print(form.cleaned_data) # obj = Post.objects.create(**form.cleaned_data) obj = form.save(commit=False) obj.user = request.user obj.save() return redirect('home') return render(request, 'posts/post_create.html', { "form": form, 'action_url': 'post_create' })
def dashboard(request): profile = Profile.objects.get(user=request.user) qs = Post.objects.filter(author=profile) connections = Profile.objects.get_all_friends_profile(request.user) if 'search_keyword' in request.GET: q = request.GET['search_keyword'] all_posts = Post.objects.filter(content__icontains=q) else: all_posts = Post.objects.all() all_jobs = Job.objects.all() related_jobs = [] connected_posts = [] print("Connections of profile : ", profile) for post in all_posts: if post.author in connections: connected_posts.append(post) for job in all_jobs: if job.work_area == profile.work_area: related_jobs.append(job) post_form = PostModelForm() job_form = JobModelForm() comment_form = CommentModelForm() post_added = False job_added = False comment_added = False if 'submit_post_form' in request.POST: post_form = PostModelForm(request.POST, request.FILES) if post_form.is_valid(): instance = post_form.save(commit=False) instance.author = profile instance.save() post_form = PostModelForm() post_added = True if 'submit_job_form' in request.POST: job_form = JobModelForm(request.POST, request.FILES) if job_form.is_valid(): instance = job_form.save(commit=False) instance.author = profile instance.save() instance.title = job_form.cleaned_data.get('title') instance.description = job_form.cleaned_data.get('description') instance.image = job_form.cleaned_data.get('image') instance.work_area = job_form.cleaned_data.get('work_area') instance.skills.add(*job_form.cleaned_data.get('skills')) instance.salary = job_form.cleaned_data.get('salary') job_form = JobModelForm() job_added = True if 'submit_comment_form' in request.POST: comment_form = CommentModelForm(request.POST, request.FILES) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.user = profile comment.post = Post.objects.get(id=request.POST.get('post_id')) comment.save() comment_form = CommentModelForm() comment_added = False context = { 'qs': qs, 'profile': profile, 'post_form': post_form, 'comment_form': comment_form, 'comment_added': comment_added, 'post_added': post_added, 'connections': connections, 'connected_posts': connected_posts, 'job_form': job_form, 'job_added': job_added, 'related_jobs': related_jobs, } return render(request, 'profiles/dashboard.html', context)