Django How to implement alert()(popup message) after complete method in view - django

I would like to have an alert() message (like in javascript) after method in view.py is complete
My method is
def change_password(request):
dictData = getInitialVariable(request)
in_username = request.POST['txt_username']
in_password = request.POST['txt_password']
in_new_password = request.POST['txt_new_password']
user = authenticate(username=in_username, password=in_password)
if user is not None:
if user.is_active:
u = User.objects.get(username=in_username)
u.set_password(in_new_password)
u.save()
# Redirect to a success page.
return HttpResponseRedirect('/profiles/'+in_username)
After u is saved to database, the popup message will be shown.
How could I implement it?

I think the best solution would be messages (docs)
As described in message levels docs Django suggests to use "INFO" level messages to communicate with users.
By default messages are enabled in Django. If my example doesn't work for you as it is you should check enable messages block
View part:
from django.contrib import messages
def change_password(request):
...your stuff...
messages.info(request, 'Your password has been changed successfully!')
return HttpResponseRedirect('/profiles/'+in_username)
Template part:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
You can paste massage output in specific view or in general templates (layout/header).

There are many ways to do this well (see "flash" in Bootstrap, for example)... but here's how you do literally what you ask about.
In the view you redirect to, pass a message value to your template:
return render_to_response('template_name', message='Save complete')
And in your template, add this script:
<script>
alert('{{ message }}');
</script>

add the messages.success after the send_mail()
from django.contrib import messages
def contact(request):
subject = request.POST['name']
message = request.POST['message']
recipient = settings.EMAIL_HOST_USER
recipient = [recipient,]
email_from = request.POST['mailing']
send_mail( subject, message, email_from, recipient )
messages.success(request, 'Successfully Sent The Message!')
return redirect('send_email')
add this code in your templates in body
{% if messages %}
{% for message in messages %}
{% if message.tags %} <script>alert("{{ message }}")</script> {% endif %}
{% endfor %}
{% endif %}

Simply redirect to /profiles/in_username/password_updated instead
you could use a HTML template on /profiles/in_username/password_updated:
This template redefines the one used in /profiles/in_username and add a javascript with alert
<!DOCTYPE html>
<html lang="en">
<body>
<script>
alert("Your message");
</script>
[...]
</body>
</html>
Simply use render_to_response in that view:
from django.shortcuts import render_to_response
return render_to_response('mytemplate.html', {"in_username": in_username});
See https://docs.djangoproject.com/en/1.7/topics/templates/ for more info on templating.

Related

Django not rendering view

I'm making a messaging system which has a message model and some views for things like inbox and conversation.
Inbox view and template works and is linked to the conversation through the id of the sender. However, when I click on the senders name (The senders name is the link (template link is {{ sender }}) that is meant to trigger the conversation view which is supposed to render the template convo.html ( the project level url for that is url(r'^messaging/inbox/conversation/(?P<id>[\w-]+)/$', message_views.Conversation.as_view(), name="conversation"),) ), Django changes the url but stays on the same page(So it triggers the inbox view and stays on inbox.html and changes the url but doesn't trigger the conversation view and doesn't render the convo.html template). I've tried changing from a function based view to a class based view and it did nothing. What am I doing wrong?
Note: I'm still new to django so forgive me for any bad practices; I'm attempting to teach myself.
views.py (inbox and conversation views)
def inbox(request):
messages = Message.objects.filter(receiver=request.user.id)
senders = {}
for message in messages:
if message.sender not in senders:
senders[message.sender] = message
return render(request, 'inbox.html', {'senders': senders})
# There is only ever one of this view, either the CBV or FBV
def conversation(request, other_user_id):
print(request.method)
if request.method == "POST":
reply = request.post['reply']
other_user = CustomUser.objects.get(id=other_user_id)
message = Message(sender=request.user, receiver=other_user)
message.save()
return redirect("conversation")
else:
other_user = CustomUser.objects.get(id=other_user_id)
messages = Message.objects.filter(Q(receiver=request.user) & Q(sender=other_user))
print(messages)
return render(request, 'convo.html', {'messages': messages})
# Class based conversation view
class Conversation(View):
def post(request, other_user_id):
reply = request.post['reply']
other_user = CustomUser.objects.get(id=other_user_id)
message = Message(sender=request.user, receiver=other_user)
message.save()
return redirect("conversation")
def get(request, other_user_id):
other_user = CustomUser.objects.get(id=other_user_id)
messages = Message.objects.filter(Q(receiver=request.user) & Q(sender=other_user))
print(messages)
return render(request, 'convo.html', {'messages': messages})
template - inbox.html
<!DOCTYPE html>
{% extends "accbase.html" %}
<!--Omitted outbox in favor of conversation-->
{% block content %}
<div class="inbox">
<h1>Inbox</h1>
<hr>
{% for sender in senders %}
{{ sender }}
<br>
{{sender.message.created_at}}
<br>
{% endfor %}
</div>
{% endblock %}
template - convo.html
<!DOCTYPE html>
<!--Template for reciever side of conversation-->
{% extends "accbase.html"%}
{% block content %}
<div class="conversation">
<h1>Conversation with {{sender}}</h1>
<p>
{% for msg in messages %}
<!--Order is descending ~~ bottom to top: newest to oldest-->
{{ msg.content }}
<br>
{% endfor %}
<hr>
<form method="POST">
<label>Reply</label>
<input type="text" name="reply">
</form>
</p>
<button type="submit">Send</button>
</div>
{% endblock %}
project level urls.py
from django.urls import path, re_path, include
from django.conf.urls import url
from messaging import views as message_views
urlpatterns = [
url(r'^messaging/inbox', message_views.inbox, name="inbox"),
url(r'^messaging/inbox/conversation/(?P<id>[\w-]+)/$', message_views.Conversation.as_view(), name="conversation"),
]
As Daniel Roseman said in comments " The problem here is that you didn't terminate the regex for your inbox URL, so it also matches the conversation URL. " so I added the needed $ to the end of the inbox url pattern and that fixed it!

How can i pass a message to a page while submitting a form?

When i submits the form i want to pass message as "form successfully submitted" to the same page "successs.html".How it possible?
class SuccessView(FormView):
template_name = 'success.html'
form_class = QuestForm
success_url='success'
def form_valid(self, form):
ob9=Logs.objects.all()
for i in ob9:
logids=i.id
str(logids)
q1= form.cleaned_data['ques']
obj1=Quest.objects.create(logid=logids,status=0,question=q1)
return super(SuccessView,self).form_valid(form)
If you use Django's messages framework, it's as simple as adding a message at any point during the call processing (this adds the message to the current request object) and it will be added to the response:
from django.contrib import messages
messages.success("Form successfully submitted")
You can do this just before your call to return super().form_valid(form).
In your template you need to add the code to diplay the messages of course, the example in the documentation shows you exactly how to do that.
The django message Framework offers different ways to do it:
from django.contrib import messages
messages.add_message(request, messages.INFO, 'Your message', extra_tags='more_tags')
# extra_tags is optional
or
messages.success(request,"Your message")
To retrieve it in your template, use something like
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

Delayed display of message from within an inclusion tag

The following problem is occurring in a large django project. I've been able to replicate the issue in a small mock-up project (code below).
I am trying to use the django messaging framework within an inclusion tag to display a message when a POST'ed form returns is_valid(). This approach has also been used in an another answer here (see 'final update' section).
The problem is that the message is not immediately displayed when the page is rendered after the POST. Instead the message appears the next time you navigate elsewhere or refresh the page after the POST response is received.
I am not receiving any errors. Everything appears to be operating normally, except for the delayed message display.
The reason for this approach is because I'm reusing multiple small forms across multiple apps and I need to use DRY principals for the GET and POST logic. This approach works perfectly - except for the issue with the delayed 'success' message display!
Really appreciate any feedback or assistance!
EDIT: To be clear the line which sets the message is in 'my_template.py':
messages.add_message(context['request'], messages.SUCCESS, "Successfully added entry")
The Demo Project:
settings.py:
...
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages"
)
...
base_layout.html:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
{% for message in messages %}<div class="alert{% if message.tags %} alert-{{ message.tags }}{% endif %}" role="alert">{{ message }}</div>{% endfor %}
{% block content %}{% endblock %}
</body>
</html>
my_template.html:
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
forms.py:
from django.forms.models import ModelForm
from app.models import ContactMessage
class ContactForm(ModelForm):
class Meta:
model = ContactMessage
fields = ['name']
index.html:
{% extends "app/base_layout.html" %}
{% load my_template %}
{% block content %}
{% my_template %}
{% endblock %}
my_template.py:
from django import template
from django.contrib import messages
from app.forms import ContactForm
register = template.Library()
#register.inclusion_tag('app/my_template.html', takes_context=True)
def my_template(context):
if context['request'].method=='GET':
return { 'form':ContactForm() }
if context['request'].method=='POST':
form = ContactForm(context['request'].POST)
if not form.is_valid():
return { 'form': form }
form.save()
messages.add_message(context['request'], messages.SUCCESS, "Successfully added entry")
return { 'form':ContactForm() }
The messaging framework works through middleware, what you need is some way of informing the posting in the same request/response cycle. You have the context variable at hand, so why not add a value to it:
if form.is_valid():
context['success']=True
else:
context['success']=False
Then in your template:
{%if success %}<div>whoohoo!</div>{%endif%}
According to Django, the messages are queued for rendering until it is cleared by renderer. (reference)
In your case, you are adding messages after {{ message }} tags in base.html has been rendered. So your message is stored until your next view when {{ message }} in base.html is rendered again.
To solve this, you can move your {{ message }} tags behind {% endblock %} of content. Another possible solution is to use javascript to append {{ message }} tags either from my_template.html or from end of base.html.

Django show message parameters in HttpResponseRedirect

I'm working on a basic web page that asks for a user's name and phone number, and if successfully submitted, it will redirect to a page that shows the user's name and phone number. I kept getting a 403 error when I try to submit the name and phone number. Here is the code:
This is the code in views.py:
def homeform(request):
if request.method == "POST":
form = PhoneForm(request.post)
if form.is_valid():
cd = form.cleaned_data
messages.success(request, "%s, you have recorded your phone number as %d. Thanks for the update.") %(cd["name"],cd["phone"])
return HttpResponseRedirect('/success/')
else:
form = PhoneForm()
return render_to_response('phone_form.html',{'form': form})
def success(request):
return render_to_response('success.html',{'messages': messages})
This is the code in urls.py:
urlpatterns = patterns('',
('^home/$',homeform),
('^success/$', success),
)
Here is the html file I'm using for the success function in the view:
<html>
<head>
<title>Success</title>
<head>
<body>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
I don't understand your question title, but if you're getting a 403, it's most likely a CSRF error (which you would have seen). You should be posting the view that POSTs, not the success view/template.
Add {% csrf_token %} inside the <form> tags on any POST forms or django will complain of CSRF errors.

Django: render_to_response to another view

How to pass the "render_to_response arguments" to an new view?
def show_list(request, id):
try:
profile = request.user.get_profile()
list = Lista.objects.get(user_profile = profile)
except Lista.DoesNotExist:
c = {}
c.update(csrf(request))
titlepage = "ooops, you don't have a list YET! click on button to create one!"
c = {'profile' : profile, 'request' : request, 'titlepage' : titlepage}
return render_to_response('/profiles/list/create/',c,context_instance=RequestContext(request))
with this last line this doesn't work, the url /profiles/list/create/ redirects to a view create_list.
Well, I know that I could write something like redirect(/profiles/list/create/) but with this I cannot pass the dictionary c.
Thanks in advance
You should use django.contrib.messages to display your message and redirect user to another view.
View code:
from django.contrib import messages
from django.shortcuts import redirect
def show_list(request, id):
try:
# normal flow
except Lista.DoesNotExist:
messages.warning("ooops, you don't have a list YET! click on button to create one!")
return redirect("/profiles/list/create/")
Somewhere in create template:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
Also remember to properly enable messages.
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))