Esempio n. 1
0
 def test_post_create_form_with_Invalid_data(self):
     form = PostCreateForm(data={
         'title': self.post.title,
         'body': '',
         'tags':'',
         'status': 'draft'})   
     self.assertFalse(form.is_valid())   
Esempio n. 2
0
 def test_post_create_form_with_valid_data(self):
     form = PostCreateForm(data={
         'title':self.post.title,
         'body': self.post.body,
         'tags':self.post.tags,
         'status': self.post.status})   
     self.assertTrue(form.is_valid())
Esempio n. 3
0
def edit_post(request, pk):
    template_name = 'blog/post_form.html'
    post = get_object_or_404(Post, pk=pk)
    form = PostCreateForm(request.POST or None, instance=post)
    if form.is_valid():
        form.save()
        return redirect('list_post')
    return render(request, template_name, {'form': form})
Esempio n. 4
0
def create_post(request):
    template_name = 'blog/post_form.html'
    form = PostCreateForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        form.instance.created_by = request.user
        form.save()
        return redirect('list_post')
    return render(request, template_name, {'form': form})
Esempio n. 5
0
def post_create(request):
    # 요청이 GET 방식이면 빈 폼을 보여준다.
    if request.method == 'GET':
        form = PostCreateForm()
        context = {'form': form}
        return render(request, 'blog/post_create.html', context)
    elif request.method == 'POST':
        data = request.POST
        form = PostCreateForm(request.POST)
        post = Post.objects.create(title=form['title_create'],
                                   text=form['text_create'],
                                   author=User.objects.first())
        return redirect('post_detail', pk=post.pk)
def post_update(request, id=None):
    """Update a post"""
    post_query = Post.objects.get(id=id)
    update_form = PostCreateForm(request.POST or None, instance=post_query)

    if request.method == 'POST':
        if update_form.is_valid():
            update_form.save()
            return redirect('detail', id=post_query.id)

    context = {'update_form': update_form}
    template = 'blog/post_update.html'

    return render(request, template, context)
Esempio n. 7
0
def post_add(request):
    form = PostCreateForm()

    if request.method == 'GET':
        context = {'form': form}
        return render(request, 'blog/post_add.html', context)
    elif request.method == 'POST':
        print("POST run...")
        form = PostCreateForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            text = form.cleaned_data['text']
            user = User.objects.first()
            post = Post.objects.create(title=title, text=text, author=user)
            return redirect('post_detail', pk=post.pk)
        else:
            context = {'form': form}
            return render(request, 'post_add.html', context)
Esempio n. 8
0
def post_add(request):
    if request.method == "POST":
        posts.create(title=request.POST['title'],
                     about=request.POST['about'],
                     image=request.FILES['file[]'],
                     published_date=timezone.now())
        return HttpResponseRedirect('/')
    form = PostCreateForm()
    context_data = {"title": "Home", 'form': form}
    return render(request, 'post.html', context_data)
Esempio n. 9
0
def post_create(request):
    ImageFormset = modelformset_factory(Images, fields=('image',))
    if request.method == 'POST':
        form = PostCreateForm(request.POST)
        formset = ImageFormset(request.POST or None, request.FILES or None)
        if form.is_valid() and formset.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            for f in formset:
                try:
                    photo = Images(post=post, image=f.cleaned_data['image'])
                    photo.save()
                    return redirect('post_list')
                except Exception as e:
                    break
            messages.success(request, f'Post created successfully!')
            return redirect('post_list')
    else:
        form = PostCreateForm()
        formset = ImageFormset(queryset=Images.objects.none())
    context = {
        'form': form,
        'formset': formset,
    }
    return render(request, 'blog/post_create.html',context)
Esempio n. 10
0
def post_create(request):
    ImageFormset = modelformset_factory(PostImages,
                                        fields=('image', ),
                                        extra=7)
    if request.method == 'POST':
        form = PostCreateForm(request.POST)
        formset = ImageFormset(request.POST or None, request.FILES or None)
        if form.is_valid() and formset.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            pk = post.id

            for f in formset.cleaned_data:
                try:
                    photo = PostImages.objects.create(post=post,
                                                      image=f['image'])
                    photo.save()
                    return HttpResponseRedirect(
                        Blog.redirect_route(blog_id=pk))
                except Exception as e:
                    break

    else:
        form = PostCreateForm()
        formset = ImageFormset(queryset=PostImages.objects.none())

    context = {'form': form, 'formset': formset}
    return render(request, 'blog/post_create.html', context)
Esempio n. 11
0
def post_create(request):
    """Creating single post"""
    if request.method == 'POST':
        create_form = PostCreateForm(request.POST)
        if create_form.is_valid():
            # post_created = create_form.save(commit=False)
            create_form.save()
            return redirect('list')

    create_form = PostCreateForm()

    context = {'create_form': create_form}
    template = 'blog/post_crate.html'

    return render(request, template, context)
Esempio n. 12
0
def post_create_view(request):
    if request.method == 'POST':
        post_form = PostCreateForm(request.POST, request.FILES)
        if post_form.is_valid():
            post = post_form.save(commit=False)
            post.author_id = request.user.id
            post.save()
            messages.success(request, f'Your post has been created!')
            return redirect(post.get_absolute_url())
    else:
        post_form = PostCreateForm()
    return render(request, 'blog/post_form.html', {'form': post_form})
Esempio n. 13
0
def post_create(request):
    if request.method == "POST":
        form = PostCreateForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_at = timezone.now()
            post.save()
            return redirect('post_detail', id=post.id)
    else:
        form = PostCreateForm()
    return render(request, 'blog/post_create.html', {'form': form})