Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Nursultan Bolatbayev
Nursultan Bolatbayev
16,774 Points

(Django) Why messages.success after posting form appears in admin page not in actual user page?

I have form and views. After submitting form post, display message(alert pop-up) appears in admin page, not in actual user page. I cant understand why?

views.py

def node_config(request):
    form = forms.NodeConfigForm
    if request.method == 'POST':
        form = forms.NodeConfigForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Configuration successfully submitted")
            return HttpResponseRedirect(reverse('nodes:config'))
    return render(request, 'nodes/nodeconfig.html', {'form': form})

forms.py

from . import models

class NodeConfigForm(forms.ModelForm):
    class Meta:
        model = models.NodeConfigLog
        fields = [
                'nodes',
                'command_logs',
        ]
        labels = {
                'nodes': 'Choose Nodes',
                'command_logs': 'Enter Commands'
        }

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,167 Points

The messages will accumulate until they are displayed. If your user template doesn't utilize a mechanism to display messages, then they'll show up on the first page that does. All of the default admin pages display all outstanding messages.

Nursultan Bolatbayev
Nursultan Bolatbayev
16,774 Points

Aaa, my fault. Missed that part. Thank you! Ok, after submitting it appears, but how to make it deletable by clicking on it?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,167 Points

There isn't a built-in manner to delete by clicking on them. They will disappear automatically on the next page load.

Nursultan Bolatbayev
Nursultan Bolatbayev
16,774 Points

But after submitting, it redirects to default page (the same page) automatically and forms are cleared, message appears. So no solution to delete display message without refreshing that page or going to another page?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,167 Points

Correct. No built-in solution. You must refresh the page or navigate to another page. It's a style/feature of Django.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,167 Points

There are other ways to change the behavior in the browser, such as adding some jQuery to alter the display. In this StackOverflow answer there is a snippet to hide the message after a given time period.

Nursultan Bolatbayev
Nursultan Bolatbayev
16,774 Points

Thank you, Chris. I will have a look and try it.

Nursultan Bolatbayev
Nursultan Bolatbayev
16,774 Points

Yes, it works, message disappears automatically after configurable time. But it doesnt allow to apply CSS together with that JQuery script.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,167 Points

Not sure why CSS can't be applied as you wish. I don't recall jQuery and CSS being incompatible.