我to_json
在一个对象上使用该方法,并试图让:methods
参数工作.我的模型(Drop)上有一个方法叫做is_favorited_by_user?
.此方法接受一个参数current_user
,然后检查用户是否收到了drop.如何通过该to_json
方法传递此参数.
render :json => @drops.to_json(:methods => :is_favorited_by_user?(current_user))
Run Code Online (Sandbox Code Playgroud) 嗨,我收到此错误,而我尝试制作一个简单的应用程序.
NameError at /first/
global name 'render_to_response' is not defined
Request Method: GET
Request URL: http://localhost:8000/first/?preview
Django Version: 1.3
Exception Type: NameError
Exception Value:
global name 'render_to_response' is not defined
Exception Location: /home/naveen/django_projects/myproject/first/views.py in index, line 5
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/home/naveen/django_projects/myproject',
'/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
'/usr/local/lib/python2.6/dist-packages',
'/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/PIL',
'/usr/lib/python2.6/dist-packages/gst-0.10',
'/usr/lib/pymodules/python2.6',
'/usr/lib/python2.6/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Thu, 31 Mar 2011 14:50:32 -0500
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?!
如何在Django render_to_response中使用HTTP响应头
我想使用Cache-Control,但无法判断它是否正常工作.以下是正确的:
render_to_response(templatename, {'Cache-Control':'no-cache'},context_instance=RequestContext(httpreq))
Run Code Online (Sandbox Code Playgroud) 我正在为Django的render_to_response()编写一个包装函数来添加CSRF处理.
逻辑是:
def some_view (request)
dictionary = {'context_param': some_param}
dictionary.update(csrf(request))
# ... view code here
return render_to_response("a_template.html", dictionary)
Run Code Online (Sandbox Code Playgroud)
render_to_response()具有以下签名:
def render_to_response(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
我想写透明包装器 - 只需添加一些功能(前面提到过)并保留其他内容.我想我应该写这样的东西:
def csrf_response (request, *args, **kwargs):
# Here I need to somehow extract dictionary from args or kwargs
if dictionary is None:
dictionary = {}
dictionary.update(csrf(request))
# And here I somehow need to pass dictionary into render_to_response() fot the furher processing
return render_to_response(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
所以问题是 - 从args/kwargs中提取所需参数(然后更改它)并将其进一步传递的最佳实践是什么?
render_to_response()的BTW代码似乎有点奇怪.这里是:
def render_to_response(*args, **kwargs):
"""
Returns a HttpResponse whose …
Run Code Online (Sandbox Code Playgroud) 您好我想要提供我内容的纯文本版本.所以我有一个单独的模板.我打电话render_to_response
用mimetype="text/plain"
,但我想告诉浏览器中打开该页面的HTTP响应内容是UTF-8编码.我该怎么做(例如我需要添加什么render_to_response
)?
您好,并提前感谢您.
我知道这是完全noob问题,我已经在各种论坛中搜索并阅读并重新阅读了文档,所以请保持温和.
我有一个观点:
#views.py
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from acme.acmetest.models import Player
from acme.acmetest.models import PickForm
def playerAdd(request, id=None):
form = PickForm(request.POST or None,
instance=id and Player.objects.get(id=id))
# Save new/edited pick
if request.method == 'POST' and form.is_valid():
form.save()
return HttpResponseRedirect('/draft/')
#return render_to_response('makepick.html', {'form':form})
return render(request, 'makepick.html', {'form':form})
def draftShow(request):
draft_list = ['1', 'hello', 'test', 'foo', 'bar']
#draft_list = Player.objects.all()
#return render_to_response('makepick.html', {'draft_list' :draft_list}, context_instance=RequestContext(request))
return render_to_response('makepick.html', {'draft_list' :draft_list})
Run Code Online (Sandbox Code Playgroud)
我试图让它渲染到模板.html页面:
#makepick.html …
Run Code Online (Sandbox Code Playgroud) 我正在Django中尝试一个简单的用户注册表单.我使用时没有生成CSRF令牌render_to_response
return render_to_response('registration/register.html',RequestContext(request,{'form':RegistrationForm()}))
在我使用的时候生成CSRF令牌 render
return render(request,'registration/register.html',{'form':RegistrationForm()})
我做错了什么 render_to_response
以下是相关的代码块
views.py
@csrf_protect
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
password=form.cleaned_data['password1'],
email=form.cleaned_data['email']
)
return HttpResponseRedirect('/register_success/')
else:
return render_to_response('registration/register.html', RequestContext(request, {'form': RegistrationForm()}))
Run Code Online (Sandbox Code Playgroud)
register.html
{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
<table border="0">
{{ form.as_table }}
</table>
<input type="submit" value="Register" />
</form>
{% endblock %}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,它将显示在html文件中.字符串中的某些单词(标记为"spc")需要以黄色背景和较大的字体显示.
我试图使用render_to_response
方法将字符串(称为tdoc)发送到html文件.我用div标签替换了字符串中的'spc'标签.假设,在更换后,部分字符串是 we would seldom be prepared to <div id="spcl">examine</div> every
.我的django代码看起来像render_to_response('a.html',{'taggeddoc':tdoc})
在我的CSS中,我有以下代码
#spcl {
background-color: #FFFF00;
font-size:15px;
}
Run Code Online (Sandbox Code Playgroud)
所以,我应该看到粗体字和黄色背景中的单词检查,但我没有看到.当我查看渲染的html的源代码时,它具有以下子字符串We would seldom be prepared to <div id="spcl">examine</div> every
而不是原始字符串.
如何以所需方式显示"检查"和类似词语?
有哪些逻辑选择:
{% if request.user.is_active %}
Run Code Online (Sandbox Code Playgroud)
如果视图没有render_to_response
带字典返回request
,则模板无法正常工作.
所以,欢迎任何想法.
//编辑
我的动机是:当您处理已安装应用程序的视图时,您没有机会修改它们.
python authentication django django-templates render-to-response
django ×7
python ×4
args ×1
csrf ×1
css ×1
django-cms ×1
http-headers ×1
httpresponse ×1
kwargs ×1
render ×1
views ×1
wrapper ×1