コード例 #1
0
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})
コード例 #2
0
ファイル: views.py プロジェクト: joli-u/django-blog
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})
コード例 #3
0
ファイル: views.py プロジェクト: timmyoder/django-blog
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})
コード例 #4
0
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)
コード例 #5
0
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})
コード例 #6
0
ファイル: views.py プロジェクト: andykhkwok/mysite
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})