python - Render context data to generic.DetailView -


how can render data or redirect context data generic.detailview. have model note

class note(models.model):     key = models.charfield(max_length=50, primary_key=true)     text = models.textfield() 

and view is

class shownote(generic.detailview):     model = note     template_name = 'notes/show_note.html'      def get(self, request, *args, **kwargs):         try:             self.object = self.get_object()         except http404:             # redirect here             return render(request, 'notes/index.html', {'error': 'note doesnt exist', })         context = self.get_context_data(object=self.object)         return self.render_to_response(context) 

url(r'^show/(?p.*)/$', views.shownote.as_view(), name='show_note'),

the page show key of note , text there button save text if changed.

def save_note(request):     key = request.post['key']     selected_note = note.objects.get(pk=key)     selected_note.text = request.post['text']     selected_note.save()     //back show_note 

how can render data {'message' : 'note saved successfully'} in 'notes/show_note.html' same primary key

you can override get_context_data method this. put below method in class based view.

def get_context_data(self, **kwargs):     data = super().get_context_data(**kwargs)     data['message'] = 'note saved successfully'     return data 

then in template

{{ message }} 

docs here.

another method use messages module django.contrib.messages.

you can use below in code

def get(self, request, *args, **kwargs): .... # code         messages.success(request, "note added successfully") 

then in templates

{% message in messages%}     {{ message }} {% endfor %} 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -