Surya Teja Karra
Surya Teja Karra

Reputation: 551

How to remove session variable in a template after it's job is done in django

I have a app called dashboard which is where I redirect all logged in users with an option to add articles by the user.

After the user hits Submit button in the form, the data is sent to /dashboard/article/save URL via POST and after the data is stored, the view returns HttpResponseRedirect to show_dashboard which renders dashboard.html with a session variable result.

In the dashboard template file, I have added a notify.js code to show acknowledgements to user. The problem is if this session var is defined, everytime the dashboard page is showed, the notification is triggered EVEN if the user didn't add an article.

(I'm new to using web frameworks so I do not know how this all works properly)

Some code:

dashboard/models.py:

class Article(models.Model):

    id = models.IntegerField(primary_key=True)
    ar_title = models.CharField(max_length=25)
    ar_data = models.CharField(max_length=500)
    user = models.ForeignKey(User,on_delete=models.CASCADE)

    def getArticleTitle(self):
        return self.title

    def getArticleData(self):
        return self.title

    def getArticleAuthor(self):
        return self.user

dashboard/urls.py:

urlpatterns = [
    url(r'^$', views.show_dashboard,name='home_dashboard'),
    url(r'^profile/save/', views.save_profile,name="save_profile"),
    url(r'^newsfeed/', views.get_newsfeed,name="newsfeed",),
    url(r'^profile/', views.show_profile,name="show_profile"),
    url(r'^article/save/', views.add_new_article,name="add_new_article"),
]

dashboard/views.py:

@login_required
def show_dashboard(request):
    return render(request,'dashboard/dashboard.html',{'form':NewArticleForm()})

def add_new_article(request):
    if(request.method == 'POST'):
        ar_title= request.POST['ar_title']
        ar_data = request.POST['ar_data']
        user = request.user
        form = NewArticleForm(request.POST)
        if(form.is_valid()):
            Article.objects.create(ar_title=ar_title,ar_data=ar_data,user=user)
            request.session["result"] = "add_article_OK"
            return HttpResponseRedirect(reverse('home_dashboard'))

dashboard.html:

{% ifequal request.session.result 'add_article_OK' %}
    <script>
        $.notify("New article added successfully", 
            {position:"bottom right","className":"success","autoHide":"yes","autoHideDelay":"3000"});
    </script>
{% endifequal %}

Now, how do I remove this session value after it has displayed the message? I know del request.session['result'] can be issued but where can I put it in this heirarchy of moves?

Upvotes: 3

Views: 2575

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599540

Do it in the show_dashboard view.

Instead of getting the value from the session in the template, pop it in the view and pass it to the template; that way you take care of getting and clearing it in one go.

@login_required
def show_dashboard(request):
    context = {
        'form': NewArticleForm(),
        'result': request.session.pop('result', None)
    }
    return render(request,'dashboard/dashboard.html',context)

...
{% ifequal result 'add_article_OK' %}

Upvotes: 4

Related Questions