Ejemplo n.º 1
0
def post_new(request):
    if request.method=='POST':
        form=PostModelForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('/')
    else:
        form =PostModelForm()
    return render(request, 'blog/post_html.html', {'form':form})
Ejemplo n.º 2
0
    def test_form(self):
        title = 'As h'
        content = 'as ad'
        # obj = PostModel.objects.create(title=title, publish=timezone.now(), content=content)

        form = PostModelForm(data={
            'title': title,
            'content': content
        })  #PostModelForm(request.POST)
        form.save()
        self.assertTrue(form.is_valid())
        PostModel.objects.get(title=form.cleaned_data.get('title'))
        self.assertEqual(form.cleaned_data.get('title'), title)
        self.assertNotEqual(form.cleaned_data.get('content'),
                            "Another content")
Ejemplo n.º 3
0
def home(request):
    # initial_dict = {
    #     # "some_text": "Text",
    #     "boolean": True,
    #     "integer": "123"
    # }
    #
    # form = TestForm(request.POST or None)
    #
    # if form.is_valid():
    #     print(form.cleaned_data)

    # if request.method == "POST":
    #     form = TestForm(data=request.POST or None)
    #     print(request.POST)
    # elif request.method == "GET":
    #     form = TestForm(user=request.user)
    #     print(request.GET)

    form = PostModelForm(request.POST or None)
    if form.is_valid():
        obj = form.save(commit=False)
        print(obj.title)
        obj.title = "some random title"
        obj.publish = timezone.now()
        obj.save()
    if form.has_error:
        print(form.errors.as_json())
        print(form.errors.as_text())

    context = {"form": form}
    return render(request, "forms.html", context)
Ejemplo n.º 4
0
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostModelForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostModelForm(instance=post)
    return render(request, 'blog/post_edit.html', {'form': form})
def post_create(request):
    form = PostModelForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.user = request.user
        instance.save()
        messages.success(request, 'successfully created')
        return HttpResponseRedirect(instance.get_absolute_url())
    else:
        messages.error(request, 'creation failed')
    context = {
        "form": form
    }
    return render(request, 'blog/form.html', context=context)
def post_model_update_view(request, id=None):
    obj = get_object_or_404(PostModel, id=id)
    form_a = PostModelForm(request.POST or None, instance=obj)
    context = {
        "form": form_a,
    }
    if form_a.is_valid():
        obj = form_a.save(commit=False)
        obj.save()
        messages.success(request, "Updated blog post!")
        # return HttpResponseRedirect("/blog/")
        return HttpResponseRedirect("/blog/{id}".format(id=obj.id))

    template = "blog/update-view.html"
    return render(request, template, context)
def post_update(request, slug):
    instance = get_object_or_404(Post, slug=slug)
    form = PostModelForm(request.POST or None,
                         request.FILES or None, instance=instance)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request, 'successfully saved')
        return HttpResponseRedirect(instance.get_absolute_url())
    else:
        messages.error(request, 'updation failed')
    context = {
        "form": form,
        "title": instance.title
    }
    return render(request, 'blog/form.html', context=context)
Ejemplo n.º 8
0
def post_new_model(request):
    if request.method == "POST":
        #java의 break와 같은 역할
        #pass
        myform = PostModelForm(request.POST)
        if myform.is_valid():
            print(myform.cleaned_data)
            post = myform.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        #get 요청 일 때 입력 폼을 출력
        myform = PostModelForm()
    return render(request, 'blog/post_edit.html', {'form': myform})
Ejemplo n.º 9
0
def post_new(request):
    if request.method == 'POST':
        form = PostModelForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)  # ModelForm 에서만 지원되는 함수
            post.title = 'auto title'
            post.save()
            messages.success(request, '포스팅을 저장했습니다.')
            return redirect('blog:index')
    else:
        form = PostModelForm()

    return render(request, 'blog/post_form.html', {
        'legend': '새 포스팅 쓰기',
        'form': form
    })
Ejemplo n.º 10
0
def post_form(request, service, id=None):
    form = None
    post = None
    if id in (None,):
        form = PostModelForm(request.POST)
    else:
        try:
            post = service.get_by_id(id)
            if request.method == 'POST':
                form = PostModelForm(request.POST, instance=post)
            else:
                form = PostModelForm(instance=post)
        except BusinessException as be:
            raise be
    post = form.save(commit=False)
    post.author = request.user
    return post, form
Ejemplo n.º 11
0
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)

    if request.method == 'POST':
        form = PostModelForm(request.POST, request.FILES, instance=post)
        if form.is_valid():
            post = form.save(commit=False)  # ModelForm 에서만 지원되는 함수
            post.title = 'auto title'
            post.save()
            messages.success(request, '포스팅을 저장했습니다.')
            return redirect('blog:index')
    else:
        form = PostModelForm(instance=post)

    return render(request, 'blog/post_form.html', {
        'legend': '새 포스팅 쓰기',
        'form': form
    })
Ejemplo n.º 12
0
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)

    if request.method == 'POST':
        form = PostModelForm(request.POST, request.FILES, instance=post)
        if form.is_valid():
            post = form.save()  # ModelForm way

            messages.success(request, '포스팅을 수정했습니다.')

            # return redirect('blog:post_detail', post.id)
            # return redirect(post.get_absolute_url())
            return redirect(post)
    else:
        form = PostModelForm(instance=post)

    return render(request, 'blog/post_form.html', {
        'form': form,
    })
Ejemplo n.º 13
0
def post_new(request):
    if request.method == 'POST':
        form = PostModelForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)  # ModelForm way
            post.author = request.META['REMOTE_ADDR']
            # 로그인 상황에서는 정상처리
            # 로그아웃 상황에서는 오류상황, request.user 는 AnonymousUser 인스턴스이기 때문
            post.writer = request.user
            post.save()

            messages.success(request, '새 포스팅을 저장했습니다.')

            # return redirect('blog:post_detail', post.id)
            # return redirect(post.get_absolute_url())
            return redirect(post)
    else:
        form = PostModelForm()

    return render(request, 'blog/post_form.html', {
        'form': form,
    })
Ejemplo n.º 14
0
def post_model_create_view(request):
    # if request.method == "POST":
    #     # print(request.POST)
    #     form = PostModelForm(request.POST)
    #     if form.is_valid():
    #         form.save(commit=False)
    #         print(form.cleaned_data)

    form_a = PostModelForm(request.POST or None)
    context = {
        "form": form_a
    }
    if form_a.is_valid():
        obj = form_a.save(commit=False)
        obj.save()
        messages.success(request, "Created a new blog post!")
        context = {
            "form": PostModelForm()
        }
        # return HttpResponseRedirect("/blog/")
        # return HttpResponseRedirect("/blog/{id}".format(id=obj.id))

    template = "blog/create-view.html"
    return render(request, template, context)
Ejemplo n.º 15
0
def create_post(request):
    form = PostModelForm(request.POST or None)
    if form.is_valid():
        post = form.save()
        return redirect(reverse("blog_index"))
    return render(request, "create_post.html", locals())
Ejemplo n.º 16
0
def blog_post(request):
    form = PostModelForm(request.POST or None)
    if form.is_valid():
        form.save()

    return render(request, 'test_form.html', {'form': form})