예제 #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
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})