def post(self, request, *args, **kwargs): form = self.form_class(request.POST) if form.is_valid(): text = form.cleaned_data['text'] user_post = UserPost(text=text, author=request.user) user_post.save() return redirect('index')
def index(request): if request.method == 'GET': posts = UserPost.objects.all() form = UserPostForm() context = { 'posts': posts, 'form': form, } return render(request, 'index.html', context) elif request.method == 'POST': form = UserPostForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] user_post = UserPost(text=text) user_post.save() return redirect('index')
def index(request): if request.method == 'GET': posts = UserPost.objects.all() form = UserPostForm() context = { 'posts': posts, 'form': form, } return render(request, 'index.html', context) elif request.method == 'POST': form = UserPostForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] user_post = UserPost(text=text, author=request.user) user_post.save() return redirect('index')
def index(request): posts = UserPost.objects.order_by('-date_added') if request.method == 'GET': form = UserPostForm() context = { 'posts': posts, 'form': form, } return render(request, 'index.html', context) elif request.method == 'POST': form = UserPostForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] author = form.cleaned_data['author'] user_post = UserPost(text=text, author=author) user_post.save() return redirect('index') return render(request, 'index.html', {'form': form, 'posts': posts,})
def index(request): posts = UserPost.objects.order_by('-date_added') context = { 'posts': posts } if request.method == 'GET': form = UserPostForm() context['form'] = form return render(request, 'index.html', context) elif request.method == 'POST': form = UserPostForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] author = form.cleaned_data['author'] user_post = UserPost(text=text, author=author) user_post.save() return redirect('index') else: context['form'] = form return render(request, 'index.html', context)