Posts

Showing posts with the label message framework

Django - To display message from Message Framework correctly

To be able to display the messages from Django's Message Framework, you have to provide the request context in the return. For example: views.py: ====== def export(request): csv_file = get_csv_file(request) if type(csv_file).__name__=='file': response = HttpResponse(csv_file, mimetype='application/csv') response['Content-Disposition'] = 'attachment; filename=mycsv.csv' return response else: if excel_file==0: messages.add_message(request, messages.WARNING, 'No thing.') return redirect('myapp.views.showall') If the csv_file is not a file, a message will be added to the message framework, and the user will be redirected back to the showall view. In the showall view, we have to return the request context: views.py: ======= def showall (request): ...      return render_to_response('showall.html', locals(), context_instance=RequestContext(request)) showall.html: ========= {% if messages ...

Messages notification in Django

Firstly, read the documentation carefully:  https://docs.djangoproject.com/en/dev/ref/contrib/messages/ Following instructions in the documentation to enable messages framework and use it in your django application. Example: views.py : ... from django.shortcuts import render_to_response from django.template import RequestContext from django.contrib import messages from my_app import forms def my_view(request):     if request.method == 'POST':         my_form = forms.MyForm(request.POST, request.FILES)         if my_form.is_valid():             my_form.save()             messages.add_message(request, messages.INFO, "Success")         else:             messages.add_message(request, messages.ERROR, "Fail")     else:         my_form = forms.MyForm()     return rend...