Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNursultan 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
Treehouse Moderator 68,423 PointsThe 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
16,774 PointsNursultan Bolatbayev
16,774 PointsAaa, my fault. Missed that part. Thank you! Ok, after submitting it appears, but how to make it deletable by clicking on it?
Chris Freeman
Treehouse Moderator 68,423 PointsChris Freeman
Treehouse Moderator 68,423 PointsThere isn't a built-in manner to delete by clicking on them. They will disappear automatically on the next page load.
Nursultan Bolatbayev
16,774 PointsNursultan Bolatbayev
16,774 PointsBut 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
Treehouse Moderator 68,423 PointsChris Freeman
Treehouse Moderator 68,423 PointsCorrect. No built-in solution. You must refresh the page or navigate to another page. It's a style/feature of Django.
Chris Freeman
Treehouse Moderator 68,423 PointsChris Freeman
Treehouse Moderator 68,423 PointsThere 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
16,774 PointsNursultan Bolatbayev
16,774 PointsThank you, Chris. I will have a look and try it.
Nursultan Bolatbayev
16,774 PointsNursultan Bolatbayev
16,774 PointsYes, it works, message disappears automatically after configurable time. But it doesnt allow to apply CSS together with that JQuery script.
Chris Freeman
Treehouse Moderator 68,423 PointsChris Freeman
Treehouse Moderator 68,423 PointsNot sure why CSS can't be applied as you wish. I don't recall jQuery and CSS being incompatible.