Re: Messages framework is not showing messages in my templates
Thanks Daniel, is just this, I forgget the context_processor on my return... thanks for all. G. On 18 Feb, 19:38, Daniel Rosemanwrote: > On Friday, February 18, 2011 10:56:54 AM UTC, Gabriel Prat wrote: > > > Hi all, I'm trying to use messages framework, I've checked that > > middleware, context processor and app is well configured (I'm running > > django development version which a standard manage.py startproject > > includes all needed stuff) > > > So, let me write a little bit of code, assume a model like: > > > class MyModel(models.Model): > > name = models.TextField(max_length = 100) > > url = models.URLField() > > > And a simple form: > > > class mymodelForm(forms.ModelForm): > > name = forms.CharField() > > url = forms.URLField() > > class Meta: > > model = MyModel > > > A basic view (assuming all needed imports in top of my views.py file): > > > def mymodel_create(request): > > from forms import mymodelForm > > if request.method == 'POST': > > form = mymodelForm(request.POST) > > if form.is_valid(): > > form.save() > > messages.success(request, _('Model has been saved')) > > else: > > form = projectForm() > > > return render_to_response('mymodel_create.html', {'form' : form}) > > > > > The context processor is not invoked, because you're not using a > RequestContext. So the messages variable is not added to your context. The > last line should be: > > return render_to_response('mymodel_create.html', {'form' : form}, > context_instance=RequestContext) > > See the documentation: > http://docs.djangoproject.com/en/1.2/ref/templates/api/#subclassing-c... > (the Note box, a couple of screens down - unfortunately there's no handy id > to link to directly) > -- > DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Messages framework is not showing messages in my templates
On Friday, February 18, 2011 10:56:54 AM UTC, Gabriel Prat wrote: > > Hi all, I'm trying to use messages framework, I've checked that > middleware, context processor and app is well configured (I'm running > django development version which a standard manage.py startproject > includes all needed stuff) > > So, let me write a little bit of code, assume a model like: > > class MyModel(models.Model): > name= models.TextField(max_length = 100) > url = models.URLField() > > And a simple form: > > class mymodelForm(forms.ModelForm): > name = forms.CharField() > url = forms.URLField() > class Meta: > model = MyModel > > A basic view (assuming all needed imports in top of my views.py file): > > def mymodel_create(request): > from forms import mymodelForm > if request.method == 'POST': > form = mymodelForm(request.POST) > if form.is_valid(): > form.save() > messages.success(request, _('Model has been saved')) > else: > form = projectForm() > > return render_to_response('mymodel_create.html', {'form' : form}) > > The context processor is not invoked, because you're not using a RequestContext. So the messages variable is not added to your context. The last line should be: return render_to_response('mymodel_create.html', {'form' : form}, context_instance=RequestContext) See the documentation: http://docs.djangoproject.com/en/1.2/ref/templates/api/#subclassing-context-requestcontext (the Note box, a couple of screens down - unfortunately there's no handy id to link to directly) -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Messages framework is not showing messages in my templates
Hi, It's been some time I haven't used the messages framework but don't you see your messages with some lags ? Like try to submit twice your form and see if you don't get the messages after the second post (and don't go in the admin between those requests). When forms are valid, I usually redirects to the same page (or another one depending on edit/create mode) and the messages show up. Regards, Xavier. Le 18 févr. 2011 à 11:56, Gabriel Prat a écrit : > Hi all, I'm trying to use messages framework, I've checked that > middleware, context processor and app is well configured (I'm running > django development version which a standard manage.py startproject > includes all needed stuff) > > So, let me write a little bit of code, assume a model like: > > class MyModel(models.Model): >name= models.TextField(max_length = 100) >url = models.URLField() > > And a simple form: > > class mymodelForm(forms.ModelForm): >name = forms.CharField() >url = forms.URLField() >class Meta: >model = MyModel > > A basic view (assuming all needed imports in top of my views.py file): > > def mymodel_create(request): >from forms import mymodelForm >if request.method == 'POST': >form = mymodelForm(request.POST) >if form.is_valid(): >form.save() >messages.success(request, _('Model has been saved')) >else: >form = projectForm() > >return render_to_response('mymodel_create.html', {'form' : form}) > > and my template (basic as well) > > {% if messages %} > > {% for message in messages %} >