Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

python - Django: After updating a post, how can i make the "updated" success message temporary, till I refresh the page. Cause now it stays after refresh

Django: Hi everyone, After updating a post, how do i redirect success message to full details page TEMPORARILY. because for my code now, after i update, i can see the updated message but even after refreshing, the updated success message remains there. kindly advise thank you!

views.py

def edit_blog_view(request, slug):

    context = {}
    blog_post = get_object_or_404(BlogPost, slug=slug)
    if request.POST:
        form = UpdateBlogPostForm(request.POST or None, request.FILES or None, instance=blog_post)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.save()
            return redirect('HomeFeed:detail', slug=slug)

    form = UpdateBlogPostForm(
            initial = {
                    "brief_description": blog_post.brief_description,
                    "image": blog_post.image,
                    "body": blog_post.body,

            }
        )

    context['form'] = form
    return render(request, 'HomeFeed/edit_blog.html', context)

def detail_blog_view(request, slug):

    context = {}
    blog_post = get_object_or_404(BlogPost, slug=slug)
    context['success_message'] = "Updated"
    context['blog_post'] = blog_post
    return render(request, 'HomeFeed/detail_blog.html', context)

detail_blog.html

{% if success_message %}
<div class="alert alert-success" role="alert"><h3 style="text-align: center;">{{success_message}}</h3></div>
{% endif %}



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The message is always gonna be rendered because you passed it as a property for your template context inside the view, the Django messages framework can help you achieve that. If you want the message auto-remove you can use Ajax for that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...