Пример #1
0
def post_update(request, pk):
    post = get_object_or_404(Post, pk=pk)

    if request.method == 'POST':
        f = PostForm(request.POST, instance=post)

        if f.is_valid():
            # if author is not selected and user is superuser, then assign the post to the author named staff
            if request.POST.get('author') == "" and request.user.is_superuser:
                updated_post = f.save(commit=False)
                author = Author.objects.get(user__username='******')
                updated_post.author = author
                updated_post.save()
                f.save_m2m()
            # if author is selected and user is superuser
            elif request.POST.get('author') and request.user.is_superuser:
                updated_post = f.save()
            # if user is not a superuser
            else:
                updated_post = f.save(commit=False)
                updated_post.author = Author.objects.get(
                    user__username=request.user.username)
                updated_post.save()
                f.save_m2m()

            messages.add_message(request, messages.INFO, 'Post updated.')
            return redirect(reverse('post_update', args=[post.id]))
    else:
        f = PostForm(instance=post)

    return render(request, 'cadmin/post_update.html', {
        'form': f,
        'post': post
    })
Пример #2
0
def post_add(request):
    if request.method == "POST":
        f = PostForm(request.POST)
        if f.is_valid():
            # if author is not selected and user is superuser,
            # then assign the post to the author named staff
            if request.POST.get('author') == "" and request.user.is_superuser:
                new_post = f.save(commit=False)
                author = Author.objects.get(user__username='******')
                new_post.author = author
                new_post.save()
                f.save_m2m()

            # if author is selected and user is superuser
            elif request.POST.get('author') and request.user.is_superuser:
                new_post = f.save()

            # if user is not a superuser
            else:
                new_post = f.save(commit=False)
                new_post.author = Author.objects.get(
                    user__username=request.user.username)
                new_post.save()
                f.save_m2m()

            messages.add_message(request, messages.INFO, 'Post added.')
            return redirect('post_add')
    else:
        f = PostForm()

    return render(request, 'cadmin/post_add.html', {'form': f})
Пример #3
0
def post_form(request):
    if request.method == 'GET':
        f = PostForm()
        context = {'postform': f}
        return render(request, 'myapp/post.html', context)
    if request.method == 'POST':
        f = PostForm(request.POST)
        if f.is_valid():
            f.save()
        return redirect(reverse('all posts'))
def newpost(request, pk):
    u = get_object_or_404(User, pk=pk)
    form = PostForm()
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            form.save(commit=False)
            form.instance.author = u
            form.save()
            return redirect("home")
    return render(request, 'myapp/newpost.html', {'form': form})
Пример #5
0
def add(request):
    if (request.method == "POST"):
        form = PostForm(request.POST, request.FILES)
        if (form.is_valid()):
            form.save()
            messages.success(request, 'Post Added Successfully')
        else:
            messages.success(request, "Please Enter Mandatory Details")

    else:
        form = PostForm()

    return render(request, "myapp/post_form.html", {'form': form})
Пример #6
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.date_published = datetime.datetime.now()
            model_instance.save()
            return redirect('list_posts')
        else:
            return render(request, "add_post.html", {'form': form})
    else:
        form = PostForm()
        return render(request, "add_post.html", {'form': form})
Пример #7
0
def update_view(request, id):
    # dictionary for initial data with
    # field names as keys
    context = {}

    # fetch the object related to passed id
    obj = get_object_or_404(Post, id=id)
    p = Post.objects.get(id=id)
    pp = p.user.profile.id

    # pass the object as instance in form
    form = PostForm(request.POST or None, request.FILES or None, instance=obj)

    # save the data from the form and
    # redirect to detail_view
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('myapp:detail', args=[pp]))

    # add form dictionary to context
    context["form"] = form

    return render(request, "myapp/post_form.html", context)
Пример #8
0
def add_data(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.speed = get_speed(request.POST['distance'],
                                   request.POST['duration'])
            post.save()
            return redirect('data_list')
        else:
            print(form.is_valid())
    else:
        form = PostForm()
    return render(request, 'myapp/add_data.html', {'form': form})
Пример #9
0
def create_view(request):
    # dictionary for initial data with
    # field names as keys
    context = {}
    u = User.objects.get(username=request.user)
    aa = u.profile.id

    # add the dictionary during initialization
    form = PostForm(request.POST or None, request.FILES or None)
    if form.is_valid():

        f = form.save(commit=False)
        f.user = u
        f.save()
        return HttpResponseRedirect(reverse('myapp:detail', args=[aa]))

    context['form'] = form
    return render(request, "myapp/post_form.html", context)