我无法理解如何从我的 vuex 状态与我的本地状态进行交互。我有一个数组,其中包含多个存储在 vuex 状态的项目。我试图将这些数据从我的 vuex 状态获取到我的组件本地状态。我使用 getter 和计算属性获取数据没有问题,但是我无法从计算属性获取相同的数据到本地状态来操作它。我的最终目标是在这个组件上建立分页。
我可以使用 getter 和计算属性获取数据。我觉得我应该在某处使用生命周期钩子。
我试图在加载任何组件之前提取数据。与在组件本身上创建生命周期挂钩相比,这似乎没有任何影响。
export default {
name: "App",
components: {},
data() {
return {
//
};
},
mounted() {
this.$store.dispatch("retrieveSnippets");
}
};
Run Code Online (Sandbox Code Playgroud)
这是一个模块 store/modules/snippets.js
const state = {
snippets: []
}
const mutations = {
SET_SNIPPETS(state, payload) {
state.snippets = payload;
},
}
const actions = {
retrieveSnippets(context) {
const userId = firebase.auth().currentUser.uid;
db.collection("projects")
.where("person", "==", userId)
.orderBy("title", "desc")
.onSnapshot(snap => {
let tempSnippets = [];
snap.forEach(doc …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用 celery 在我的一个模型上每天检查日期。我的一个模型包含一个到期日期和一个布尔字段,表明他们的保险是否已过期。
模型很大,所以我要发布一个精简版。我想我有两个选择。要么在模型方法上运行 celery 任务,要么在我的 tasks.py 中重写该函数。然后我需要使用 Celery beat 来运行日程表来每天检查。
我有这个功能可以工作,但我直接传递了我认为错误的模型对象。
我也遇到了如何在 celery.py 中的 celery beat 调度程序中使用 args 的问题。
我真的很接近让这个工作,但我想我会以错误的方式执行任务。我认为在模型方法上执行任务可能是最干净的,我只是不确定如何完成它。
模型.py
class CarrierCompany(models.Model):
name = models.CharField(max_length=255, unique=True)
insurance_expiration = models.DateTimeField(null=True)
insurance_active = models.BooleanField()
def insurance_expiration_check(self):
if self.insurance_expiration > datetime.today().date():
self.insurance_active = True
self.save()
print("Insurance Active")
else:
self.insurance_active = False
self.save()
print("Insurance Inactive")
Run Code Online (Sandbox Code Playgroud)
任务.py
from __future__ import absolute_import, unicode_literals
from celery.decorators import task
from datetime import datetime, date
from django.utils import timezone
from .models import CarrierCompany
@task(name="insurance_expired")
def insurance_date():
carriers …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python/Django/DRF 接受来自 Typeform 的表单响应,但由于无法使哈希匹配,因此无法验证 webhook 请求。
以下是 Typeform 的说明:
1. Using the HMAC SHA-256 algorithm, create a hash (using created_token as a key) of the entire received payload as binary.
2. Encode the binary hash in base64 format.
3. Add prefix sha256= to the binary hash.
4. Compare the created value with the signature you received in the Typeform-Signature header from Typeform.
Run Code Online (Sandbox Code Playgroud)
认证文件
class TypeformAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
typeform_signature = request.META.get('HTTP_TYPEFORM_SIGNATURE')
data = request.body
secret_key = os.environ.get('TYPEFORM_SECRET_KEY')
if not …Run Code Online (Sandbox Code Playgroud) 我有一个视图,我试图从几个不同的地方显示数据。我的想法是通过 Ajax 执行此操作,因为我无法在此页面加载时执行 3 或 4 个 url。
我可以使用 Django Rest Framework 将数据获取到页面,并且我可以使用我的 ajax 请求在控制台中查看正确的数据。我只是不知道如何将这些数据放入我的 html 模板以开始显示它。通常我只会通过视图传递上下文,但这是未知领域。
我想我在 Jquery 中遗漏了一些非常小的东西,因为我对 javascript 和 Jquery 还很陌生。
视图.py
class LoadLaneHistoricalCarriers(APIView):
authentication_classes = ()
permission_classes = ()
def get(self, request, pk):
load = Load.objects.get(pk=pk)
load_state = load.shipper.state
load_equipment_type = load.equipment_type
historical_loads_in_state = Load.objects.filter(shipper__state=load_state)
carriers = CarrierCompany.objects.filter(state=load_state)
historical_carriers = []
historical_loads = []
for load in historical_loads_in_state:
historical_loads.append(load.id)
historical_carriers.append(load.carrier.name)
data = {
'historical_loads': historical_loads,
'historical_carriers': historical_carriers
}
return Response(data)
Run Code Online (Sandbox Code Playgroud)
模板这是一个模态,以防万一。
<script>
var endpoint = …Run Code Online (Sandbox Code Playgroud) 我在前端使用 Vue,后端使用 Python/Django。我想编写测试以确保我对后端的 API 调用按预期工作,但我无法模拟 Axios 调用。
我可能设置错误,但您可以看到我有一个函数,该函数旨在在组件“创建”钩子内部调用。因此,在创建组件时,此函数会调用我的后端,以根据 URL 附带的查询参数检索信息。这一切都有效,但我想学习如何模拟此 API 请求以编写更好的测试。
API Service在整个应用程序中用于调用后端。
文件路径:src/api/api.js
import axios from "axios";
import { djangoServiceUser } from "../../config.js";
export default axios.create({
baseURL: "/api",
timeout: 5000,
headers: {
"Content-Type": "application/json",
Authorization: `Token ${djangoServiceUser}`
}
});
Run Code Online (Sandbox Code Playgroud)
单文件组件
这确实有效:
<script>
import api from "@/api/api";
export default {
data() {
return {
loading: false,
userBusinessOptions: null
};
},
methods: {
fetchBusinesses(fwt_user_id) {
this.loading = true;
api.get(`companies/${fwt_user_id}`).then(
response => {
this.loading = false;
let businessNames = []; …Run Code Online (Sandbox Code Playgroud) django ×3
vue.js ×2
ajax ×1
axios ×1
celery ×1
celery-task ×1
celerybeat ×1
javascript ×1
jestjs ×1
jquery ×1
python ×1
python-3.x ×1
typeform ×1
vuejs2 ×1
vuex ×1