'字典'对象没有属性' HTTP_REFERER'

时间:2016-11-16 08:23:45

标签: python django dictionary

我一直试图在这个答案后找到上一个网址:

Django request to find previous referrer

所以,在我的.py中我做了:

print request.META
print request.META.HTTP_REFERER
print request.META['HTTP_REFERER']

request.META 返回:

{'RUN_MAIN': 'true', 'HTTP_REFERER': 'http://127.0.0.1:8000/info/contact/', 'XDG_GREETER_DATA_DIR': '/var/lib/lightdm-data/user', 'QT4_IM_MODULE': 'xim',....

所以,我可以看到HTTP_REFERER存在,但是当我尝试访问它时,我得到错误:

<type 'exceptions.AttributeError'>
'dict' object has no attribute 'HTTP_REFERER'

我如何访问它?

3 个答案:

答案 0 :(得分:3)

public void scheduleManagementEmail(ManagementEmailConfig managementEmailConfig, Scheduler scheduler) throws SchedulerException { logger.debug("Scheduling Management Email " + managementEmailConfig.getManagementEmailConfigId()); String jobKey = "SendManagementEmailJob_" + managementEmailConfig.getManagementEmailConfigId(); Class<? extends Job> jobClass = SendManagementEmailJob.class; JobDetail job = JobBuilder.newJob(jobClass).withIdentity(new JobKey(jobKey)).build(); Trigger trigger = sendManagementEmailJob.getTriggerWithSchedule(managementEmailConfig); trigger.getJobDataMap().put("managementEmailConfigId", managementEmailConfig.getManagementEmailConfigId()); if (!scheduler.checkExists(new JobKey(jobKey))) { scheduler.scheduleJob(job, trigger); } debugPrintJobs(); } public void debugPrintJobs() { try { logger.debug("Quartz Jobs"); Scheduler s_scheduler = this.getJobScheduler(); List<JobExecutionContext> currentJobs = s_scheduler.getCurrentlyExecutingJobs(); for (JobExecutionContext jobCtx : currentJobs) { JobKey jobKey = jobCtx.getJobDetail().getKey(); JobDetail jobDetail = s_scheduler.getJobDetail(jobKey); List<? extends Trigger> triggers = s_scheduler.getTriggersOfJob(jobKey); Date nextFireTime = null; if (triggers.size() > 0) { nextFireTime = triggers.get(0).getNextFireTime(); } logger.debug("Name= "+ jobKey.getName() + " Group=" + jobKey.getGroup() + " NextFireTime=" + nextFireTime); } } catch (Exception e) { logger.debug("debugPrintJobs:" + e.getMessage()); } 是一本字典。

时你的代码失败了
request.META

因为您尝试访问print request.META.HTTP_REFERER 作为属性,但您应该将其作为项目

进行访问
HTTP_REFERER

如果您不确定print request.META['HTTP_REFERER'] 中是否包含此项目,可以使用request.META方法作为其他回复者建议

get

如果print request.META.get('HTTP_REFERER', '') 位于request.META['HTTP_REFERER'],则会输出HTTP_REFERER,否则会打印request.META作为''的第二个参数。 get将是默认值。

答案 1 :(得分:3)

我建议:

request.META.get('HTTP_REFERER')

答案 2 :(得分:2)

尝试: <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12 search-panel"> @using (Html.BeginForm("Index", "UserManagement", FormMethod.Get)) { <div class="input-group form-group ui-widget"> @Html.TextBox("id", ViewBag.CurrentFilter as string, new { @class = "form-control autocomplete", @placeholder = "Search for..." }) <span class="input-group-btn"> <input type="submit" value="Search" class="form-control autocomplete " /> </span> </div> } </div>