示例#1
0
 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')
示例#2
0
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')
示例#3
0
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')
示例#4
0
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,})
示例#5
0
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)