Пример #1
0
def add_post_view(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.save()
            return redirect(list_view)
    else:
        form = PostForm()
        return render(request, "blogging/some_template.html", {'form': form})
Пример #2
0
def add_model(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.timestamp = timezone.now()
            model_instance.save()
            return redirect('/')
    else:
        form = PostForm()
        return render(request, "my_template.html", {'form': form})
Пример #3
0
def add_post(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.published_date = timezone.now()
            new_post.save()
            return redirect("/")

    form = PostForm()
    return render(request, "blogging/add_post.html", {"form": form})
Пример #4
0
 def post(self, request):
     post = Post()
     post.user = request.user
     form = PostForm(request.POST, instance=post)
     if form.is_valid():
         post = form.save()
         form = PostForm()
         url = reverse("post_detail_page", args=[post.pk])
         message = "Post created successfully! "
         message += '<a href="{0}">View</a>'.format(url)
         messages.success(request, message)
     return render(request, "post_form.html", {'form': form})
Пример #5
0
def create_post(request):
    print('model form request test')
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.timestamp = timezone.now()
            model_instance.save()
            return redirect('/')
    else:
        form = PostForm()
        return render(request, 'blogging/post.html', {'form': form})
Пример #6
0
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()

            return redirect('/')
    else:
        form = PostForm()
        return render(request, "blogging/post_edit.html", {'form': form})
Пример #7
0
def add_post(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.created_date = datetime.datetime.now()
            model_instance.published_date = datetime.datetime.now()
            model_instance.save()
            return redirect('/')

    else:
        form = PostForm()
        return render(request, "modelform.html", {'form': form})
Пример #8
0
def add_model(request):
    """View to add a new entry to the Post database. Works with form.py."""

    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.timestamp = timezone.now()
            model_instance.save()
            return redirect('/')

    else:
        form = PostForm()
        return render(request, "blogging/post-form.html", {'form': form})
Пример #9
0
def add_model(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.author = request.user
            model_instance.published_date = timezone.now()
            model_instance.save()
            return redirect("/")

    else:
        print(request.user)
        if request.user.is_anonymous:
            return redirect("../accounts/login/")
        form = PostForm()
        return render(request, "blogging/add.html", {"form": form})
Пример #10
0
def add_model(request):
    if str(request.user) == "AnonymousUser":
        return redirect("/accounts/login/")
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.author = request.user
            model_instance.timestamp = timezone.now()
            model_instance.save()
            return redirect('/')

    else:

        form = PostForm()

        return render(request, "blogging/post_add.html", {'form': form})
Пример #11
0
def add_model(request):
    # If nobody is logged in, redirect to login screen
    if str(request.user) == 'AnonymousUser':
        return redirect('http://localhost:8000/accounts/login/')

    elif request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)

            # Automatically makes the logged in user the author
            model_instance.author = request.user

            # Gets the current date/time for published date
            model_instance.published_date = timezone.now()

            model_instance.save()
            return redirect('/')

    else:
        form = PostForm()

        return render(request, 'blogging/create_post.html', {'form': form})
Пример #12
0
 def get(self, request):
     form = PostForm()
     return render(request, "post_form.html", {'form': form})