使用某些上下文重定向到同一视图

时间:2014-04-11 09:59:48

标签: django django-templates django-views httpresponse

我正试图通过我的观点重定向到一个网址以及一些上下文数据。我怎么能这样做?

的url: 项目urls.py:

url(r'^employee/(?P<emp_code>[0-9a-zA-Z-_@.\s]+)/', include('apps.organisation_roles.urls',namespace='organisation_roles')),

organisation_roles / urls.py:

url(r'^reward-employee/$', RewardEmployee.as_view(), name='reward_employee'),

的观点:

def post(self, request, document=None, *args, **kwargs):
admin_user_id = OrganisationUser.objects.get(organisation_employees=request.user.organisation_user).pk
            Notification.objects.create_and_send(notification_type='ON', send_sms=False, user_id=admin_user_id,
                                                 additional_dict={
                                                     "organisation_name": request.user.organisation_user.organisation.get_name(),
                                                     "message": "You are currently low on gems and your employees are wishing to reward other employees. Please purchase more gems to continue using our services."})
#return render(request,"reward_employee.html",{"alert": "Not enough gems. You cannot reward at present. Contact your organisation."})
        return HttpResponseRedirect(reverse('organisation_roles:reward_employee',kwargs={'emp_code':self.kwargs['emp_code'],"alert": "Not enough gems. You cannot reward at present. Contact your organisation."}))
        #response = HttpResponseRedirect(request.path)
        #response.set_cookie('alert', 'Not enough gems. You cannot reward at present. Contact your organisation.')
        #return HttpResponseRedirect(request.path)

模板:

<script>
{% if alert %}
            alert("{{ alert }}");
        {% endif %}
</script>

而不是'reward_employee.html',我想给我的观点提供网址。

3 个答案:

答案 0 :(得分:0)

使用return HttpResponseRedirect而不是render。

https://docs.djangoproject.com/en/dev/intro/tutorial04/

答案 1 :(得分:0)

由于您有简单的提醒,您可以在cookie中设置数据并返回响应。

response = HttpResponseRedirect(URL)
response.set_cookie('alert', 'Not enough gems. You cannot reward at present. Contact your organisation.') 
return HttpResponseRedirect(url)

重定向到网址后,您可以获取数据并删除Cookie。

if request.COOKIES.has_key( 'alert' ):
    alert = request.COOKIES[ 'alert' ]
    del request.COOKIES[ 'alert' ]

通过这种方式,您可以在重定向到不同的URL后访问数据。

答案 2 :(得分:0)

我终于要发出警报以及重定向到同一页面。我所要做的就是创建一个具有布尔值的会话,并根据传递一些上下文,并在重定向到同一视图后显示警报。这就是我所做的。

查看:

def get_context(self, request, *args, **kwargs):
        if not request.session.get('alert'):
            tags = Tag.objects.filter(creator__organisation=request.user.organisation_user.organisation) | \
                   Tag.objects.filter(creator=None)
            context_dict = {'tags': tags, 'employees': request.user.organisation_user.get_employees_managed()}
        else:
            request.session['alert'] = False
            tags = Tag.objects.filter(creator__organisation=request.user.organisation_user.organisation) | \
                   Tag.objects.filter(creator=None)
            context_dict = {'tags': tags, 'employees': request.user.organisation_user.get_employees_managed(),'alert':"Not enough gems. You cannot reward at present. Contact your organisation."}
        return context_dict

@transaction.atomic
    def post(self, request, document=None, *args, **kwargs):
admin_user_id = OrganisationUser.objects.get(organisation_employees=request.user.organisation_user).pk
            Notification.objects.create_and_send(notification_type='ON', send_sms=False, user_id=admin_user_id,
                                                 additional_dict={
                                                     "organisation_name": request.user.organisation_user.organisation.get_name(),
                                                     "message": "You are currently low on gems and your employees are wishing to reward other employees. Please purchase more gems to continue using our services."})
            request.session['alert'] = True
            return HttpResponseRedirect(reverse('organisation_roles:reward_employee',args=(self.kwargs['emp_code'],)))

在我写的模板中,

<script>
{% if alert %}
            alert("{{alert }}");
        {% endif %}
</script>