将值从视图传递到控制器,并显示在模板中 [英] Pass Value from View to controller and display in template

查看:94
本文介绍了将值从视图传递到控制器,并显示在模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据库字段中的最新值传递给控制器​​。我也是因为从控制器的视图获得的值执行某些计算。我不知道如何使用视图中的值到控制器。我是新来的django。任何帮助将不胜感激。以下是我的代码,

I need to pass the latest value from a database field to the controller. I'm also in due to perform certain calculations with respect to the value I get from the view in the controller. I'm not aware how to use the value from the view to the controller. I'm new to django. Any help would be appreciated. Below is my code,

在我的views.py

In my views.py

class SiverifyAddReviewView(JSONResponseMixin, TemplateView):
        template_name = "siverify_add_review.html"

def get_context_data(self,*args, **kwargs):
            latest_id = SiverifyVerificationSiteRevision.objects.order_by('-id').first().id
            context = super(SiverifyAddReviewView, self).get_context_data(*args, **kwargs)
            print 'latest_id', latest_id
            context['reviewtit'] = 'Review_' + latest_id + '_' + time.strftime(%Y%m%d)
            context['ngapp'] = 'ReviewMod'
            return context

在下面的一行中,SiverifyVerificationSiteRevision是模型名称,我需要字段ID的最新值。

In the below line, SiverifyVerificationSiteRevision is the model name and I need to the latest value of the field id.

l_id = SiverifyVerificationSiteRevision.objects.latest('id')

这是从模型中获取最新值的正确方法吗?而对于控制器,我需要执行以下功能。

Is this the right method to get the latest value from the model? And Over to controller, I need to perform the below function.

控制器:

{{ngapp}}.controller(
        "SiVerifyAddReviewController",
        function($scope, $http, $modalInstance, r_header, context){
$scope.today= function dat() {
                    var d = new Date();
                    return 'Review_' + **l_id** + ((d.getFullYear() + ' ' + (d.getMonth() + 1) + ' ' + d.getDate()).replace(/ /g, ''));}
            $scope.arform['revtitle']= $scope.today();}

如在return语句( l_id )中,我需要从视图中获取最新的值并获取模板,我的模板代码是

As in the return statement( l_id), I need to get the latest value from the views and get in the template. My template code is,

模板:

<tr>
              <td><label>Review Title/Purpose*</label></td></br>
              <td><input type="text" class="col-md-10" maxlength="256" ng-model="arform.revtitle" required/></td>
          </tr>

我确定应该有一种方法从视图中获取值到控制器,我经历了几个问题标签,例如从视图传递值到控制器。但没有一个似乎与我的需要相关联。欢迎任何方法。谢谢。

I'm sure there should be a way to get the value from the view to the controller, I had gone through several Question tags such as Passing Value from View to Controller. But none of them seem to correlate with my need. Any methods are welcome. Thanks.

推荐答案

您是否只需按日期或最新的插入数据库?这有点不一样。

Do you need just the latest by date or latest by insert into DB? That's kinda different.

def get_context_data(self, **kwargs):

   latest_id = SiverifyVerificationSiteRevision.objects.latest('date').id  # `latest` method works on date fields.

   # This one gives you latest ID by insert.
   # latest_id = SiverifyVerificationSiteRevision.objects.order_by('-id').first().id

   kwargs['latest_id'] = latest_id
   kwargs['ngapp'] = 'ReviewMod'

   return kwargs

这篇关于将值从视图传递到控制器,并显示在模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆