我一直和现在几乎每个Django Framework用户都使用Django Rest Framework来创建REST API.我正在使用它使用django-rest-framework-jwt进行令牌认证,并在用户通过我们的其余API登录时返回令牌.
所以问题是如何保护我们的API端点的任何注册或登录视图.任何高级XSS脚本都可能有恶意循环来创建注册.我们如何在Django Rest Framework中保护它?
我的settings.py中有这段代码..
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
Run Code Online (Sandbox Code Playgroud)
我尝试在heroku bash中使用它设置环境变量
export EMAIL_HOST_USER=xxx
Run Code Online (Sandbox Code Playgroud)
并且
heroku config:set EMAIL_HOST_USER=xxx
Run Code Online (Sandbox Code Playgroud)
这两种方法都不适合我。如何在heroku中设置环境变量?
我需要处理从 db 对象生成的一些文件,并且在所需的过程之后需要删除该目录和文件。我决定使用 python Templefile 包。我已经尝试过但坚持了下来Direcotry not Empty [ Error 66 ].
在views.py中
def writeFiles(request, name):
tmpdir = tempfile.mkdtemp()
instance = request.user.instances.get(name=name)
print(instance)
print(instance.name)
code = instance.serverFile
jsonFile = instance.jsonPackageFile
docker = """
FROM node
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/ap
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "node", "server" ]"""
# Ensure the file is read/write by the creator …Run Code Online (Sandbox Code Playgroud) 我想将 Post 表单添加到我的 django 项目中,但 FileFiled 有问题。这是我的代码:
表格.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = [
'author',
'image',
'title',
'body'
]
Run Code Online (Sandbox Code Playgroud)
模型.py
class Post(models.Model):
author = models.ForeignKey('auth.User')
image = models.FileField(default="", blank=False, null=False)
title = models.CharField(max_length=200)
body = models.TextField()
date = models.DateTimeField(default=timezone.now, null=True)
def approved_comments(self):
return self.comments.filter(approved_comment=True)
def __str__(self):
return self.title
Run Code Online (Sandbox Code Playgroud)
如果有帮助。我也设置enctype="multipart/form-data了<form>
感谢帮助。
我在context_processors.py中有这个代码
class ContactFormView(FormView):
form_class = ContactForm
template_name = "blog/contact.html"
success_url = "/contact/"
def form_valid(self,form):
contact_name = form.cleaned_data.get('contact_name')
contact_email = form.cleaned_data.get('contact_email')
form_content = form.cleaned_data.get('content','')
try:
send_mail(contact_name,form_content,contact_email,[settings.EMAIL_HOST_USER], fail_silently=False)
except BadHeaderError:
return HttpResponse('Invalid Header Found')
return super(ContactFormView,self).form_valid(form)
Run Code Online (Sandbox Code Playgroud)
我希望通过使用上下文处理器将其包含在所有视图中.我收到此错误:
TypeError at /
__init__() takes exactly 1 argument (2 given)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.7
Exception Type: TypeError
Exception Value:
__init__() takes exactly 1 argument (2 given)
Exception Location: C:\Python27\lib\site-packages\django-1.8.7-py2.7.egg\django\template\context.py in bind_template, line 241
Python Executable: C:\Python27\python.exe
Python Version: …Run Code Online (Sandbox Code Playgroud)