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)
def post_new(request): # get -> form # post -> save form = PostModelForm() new_post = None msg = Msg(MSG_TYPE_WARNING, BLOG_WARNING_OOOPS, None) service = PostService() if request.method == 'POST': try: new_post, form = post_form(request, service) if not form.is_valid(): msg.msg = BLOG_POST_INVALID else: new_post = service.save(new_post) request.session['msg'] = { 'type': MSG_TYPE_SUCCESS, 'title': BLOG_SUCCESS_WELL_DONE, 'msg': BLOG_POST_SAVED } return redirect('blog:post_edit', id=new_post.id) except BusinessException as be: # invalid post # form.is_valid() msg.msg = '%s' % be context_response = { RESPONSE_FORM: msg, RESPONSE_FORM: form, RESPONSE_POST: new_post, } return render(request, blog_themed_view(VIEW_TPL_POST_NEW), context_response)
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})
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 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")
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)
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 })
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})
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 })
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, })
def post_form_upload(request): if request.method == 'GET': form = PostModelForm() else: # A POST request: Handle Form Upload form = PostModelForm(request.POST) # Bind data from request.POST into a PostForm # If data is valid, proceeds to create a new post and redirect the user if form.is_valid(): title = form.cleaned_data['title'] text = form.cleaned_data['text'] author = form.cleaned_data['author'] pub_date = form.cleaned_data['pub_date'] post = m.Blog.objects.create(title=title,pub_date=pub_date,text=text,author=author) action.send(request.user, verb='activity created') post_save.connect(my_handler, sender=Blog) return HttpResponseRedirect('/up(post)') return render(request, 'form_upload.html', { 'form': form, })
def post_edit(request, id): # get -> form # post -> save form = PostModelForm() edit_post = None msg = Msg(MSG_TYPE_WARNING, BLOG_WARNING_OOOPS, None) if RESPONSE_MSG in request.session: msg = Msg(**request.session[RESPONSE_MSG]) del request.session[RESPONSE_MSG] service = PostService() if request.method == 'POST': try: edit_post, form = post_form(request, service, id=id) if not form.is_valid(): msg.msg = BLOG_POST_INVALID else: edit_post = service.save(edit_post) msg = Msg(MSG_TYPE_SUCCESS, BLOG_SUCCESS_WELL_DONE, BLOG_POST_SAVED) except BusinessException as be: # invalid post # not form.is_valid() msg.msg = '%s' % be elif request.method == 'GET': try: edit_post, form = post_form(request, service, id=id) except BusinessException as be: msg = warning_msg_from_business_exception(be) context_response = { RESPONSE_MSG: msg, RESPONSE_FORM: form, RESPONSE_POST: edit_post, } return render(request, blog_themed_view(VIEW_TPL_POST_EDIT), context_response)
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, })
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)
def blog_post(request): form = PostModelForm(request.POST or None) if form.is_valid(): form.save() return render(request, 'test_form.html', {'form': form})
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())