def add_model(request): if request.method == 'POST': form = MyPostForm(request.POST) if form.is_valid(): model_instance = form.save(commit=False) model_instance.timestamp = timezone.now() model_instance.save() return redirect('/') else: form = MyPostForm() return render(request, 'blogging/new_post.html', {'form': form})
def add_model(request): if request.method == "POST": form = MyPostForm(request.POST) if form.is_valid(): model_instance = form.save(commit=False) model_instance.timestamp = timezone.now() model_instance.save() return redirect('/') else: form = MyPostForm() return render(request, "my_template.html", {'form': form})
def add_model(request): if request.method == "POST": form = MyPostForm(request.POST) if form.is_valid(): model_instance = form.save(commit=False) model_instance.published_date = timezone.now() model_instance.save() return redirect("/") else: form = MyPostForm() return render(request, "blogging/add.html", {"form": form})
def add_model(request): if request.method == "POST": try: form = MyPostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = request.user post.published_date = timezone.now() post.save() return redirect("/") except ValueError: return render(request, "blogging/error-login.html") else: # GET form = MyPostForm() context = {"form": form} return render(request, "blogging/add.html", context)
def add_model(request): if request.method == "POST": form = MyPostForm(request.POST) if form.is_valid(): model_instance = form.save(commit=False) model_instance.published_date = timezone.now() model_instance.author = User.objects.get(username="******") model_instance.save() return redirect("/") else: form = MyPostForm() return render(request, "add.html", {"form": form})
def add_model(request): if request.method == "POST": form = MyPostForm(request.POST) if form.is_valid(): model_instance = form.save(commit=False) if model_instance.created_date is None: model_instance.created_date = timezone.now() model_instance.modified_date = timezone.now() model_instance.published_date = timezone.now() else: model_instance.modified_date = timezone.now() model_instance.published_date = timezone.now() model_instance.save() return redirect('/') else: form = MyPostForm() return render(request, "blogging/my_template.html", {'form': form})