小编Lal*_*s M的帖子

如何使用组件的 onclick 事件重定向到 gatsby-react 页面?

我正在尝试使用onClick组件的事件重定向到页面。因为我有 react gatsby 安装,所以可以使用Linkfromgatsby-link重定向。

import React from  'react';
import { OverflowMenu, OverflowMenuItem } from '../Overflow';
import Link from 'gatsby-theme-carbon/src/components/Link';

class  OverflowComponent extends React.Component {

editPage(index) {
  console.log();
  // window.location.href='/edit';
  return(
    <Link to="/edit"></Link> // I'm trying to redirect to Edit page
  )
}

deletePage() {
  console.log("Delete clicked");
}

  render(){ 
    return (
      <div>
        <OverflowMenu flipped={true}>
            <OverflowMenuItem  itemText="Edit" primaryFocus onClick={() => this.editPage()} />
            <OverflowMenuItem  itemText="Delete" onClick={() => this.deletePaget()} />
        </OverflowMenu>
      </div>
    );
  }
}

export default OverflowComponent; …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs gatsby

9
推荐指数
1
解决办法
1万
查看次数

如何以编程方式将条件字段添加到 wagtail 页面类型?

I\xe2\x80\x99m 尝试将条件字段添加到 wagtail 页面类型模型,条件字段可能如下图所示。有两个字段:QuestionAnswer。应根据问题字段的选择显示答案字段

\n\n

在此输入图像描述

\n\n

如果我们从问题中选择丰田,那么凯美瑞陆地巡洋舰应该显示在答案下拉列表中,如果我们选择本田,那么思域雅阁应该显示在答案中。

\n\n

在blocks.py中我有两个类分别负责问题答案字段

\n\n
class ConditionsQuestionBlock(blocks.ChooserBlock):\n  widget = Select\n  class Meta:\n    icon = "question"\n\n  @cached_property\n  def target_model(self):\n    return Question\n\n  @cached_property\n  def field(self):\n    return forms.ModelChoiceField(\n        queryset=Question.objects.all(),\n        widget=self.widget,\n        required=self._required,\n    )\n\n  def value_for_form(self, value):\n    if isinstance(value, User):\n        return value.pk\n    else:\n        print("selected q:",value)\n        selectedqval=value\n        print("selected qvalue:",selectedqval)\n        return value\n\nclass ConditionsAnswerBlock(blocks.ChooserBlock):\n  widget = Select\n  class Meta:\n    icon …
Run Code Online (Sandbox Code Playgroud)

django wagtail

7
推荐指数
1
解决办法
1009
查看次数

如何在页面 API 中获取图像 url 或下载图像的 url,其中图像是由流场创建的?

在我的 wagtail 应用程序中,我有一个流字段,用于使用 ImageChooserBlock 上传图像以及标题和文本。这意味着在单个流字段中我有一个标题、一个文本和一个图像上传输入。我正在尝试在其余框架的页面 API ( ) 中获取图像 url localhost:8000/api/v2/pages/[page-id]。但是这个pages api只给出了上传图片的image id,如下

{
    "type": "avengers",
    "value": {
        "title": "Tony Stark",
        "avengers": [
            {
                "image": 1,            /******* this is the image id returned ********/
                "title": "Iron Man",
                "text": "Iron man is now in framework"
            }
        ]
    },
    "id": "2f27cb24"
} 
Run Code Online (Sandbox Code Playgroud)

如果我访问 images api( http://localhost:8000/api/v2/images/1/) 我会得到download_url如下信息

{
    "id": 1,
    "meta": {
        "type": "wagtailimages.Image",
        "detail_url": "http://localhost/api/v2/images/1/",
        "tags": [],
        "download_url": "/media/original_images/avenger.jpeg"
    },
    "title": "avenger.jpeg",
    "width": 400,
    "height": 400
} …
Run Code Online (Sandbox Code Playgroud)

django django-rest-framework wagtail

5
推荐指数
1
解决办法
1868
查看次数

如何将值从子功能组件传递到父类组件?

我有一个父类组件和一个子功能组件。我们如何将值从这种类型的子组件传递到父组件 - 我见过一些将值从子类组件传递到父类组件的示例。

父组件

import React from "react";
import ChildComponent from "./ChildComponent/ChildComponent";

class ParentComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: [] // An index value is in this state
        };
    }

    render(){
        //can console log the return value from the ChildComponent here if needed 
        return(
            <React.Fragment>
                <ChildComponent indexval = {this.state.data.index} />                                
            </React.Fragment>
        )
    }
}

export default ParentComponent; 
Run Code Online (Sandbox Code Playgroud)

一个 propindexval被传递给子组件,它是一个功能组件,如下所示

子组件

import React from 'react';;
import Button from './Button/Button';

function ChildComponent(props) {
    const newVal=(index) …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

5
推荐指数
1
解决办法
4749
查看次数

使用useEffect时如何等待渲染完成以获取useContext?

我有一个从UserPage 如下所示的到外部身份验证服务的条件重定向 。条件是这样的,如果我的UserContext不包含用户对象,那么页面将被重定向到 https://urltoauthenticate:5000/user 进行用户身份验证。条件在第一次运行时满足并进入认证服务

但是问题是在成功认证后发生的。在身份验证成功的情况下,即使UserContext提供了用户对象,它在第一次渲染中也会为空。在这里,我如何等待用户对象可用并准备就绪,以便页面不会再次重定向到身份验证服务?

import React, { useEffect, useContext } from 'react';
import { UserContext } from '../context/UserContext';
export default function UserPage () {
  const user = useContext(UserContext)
  useEffect(() => {
    if(user === null{
      window.location.assign("https://urltoauthenticate:5000/user");
    }
  }, [users]);
  return(
    <div>Hello {user.name}</div>
  )
}
Run Code Online (Sandbox Code Playgroud)

reactjs react-hooks

5
推荐指数
1
解决办法
339
查看次数

如何返回 wagtail 应用程序的 model.py 文件中的当前页面 URL

我有一个 wagtail 应用程序,我需要在其中返回当前页面的 url。我的应用程序的 model.py 的代码如下

import urllib

from django.conf import settings
from django.db import models

from wagtail.core.models import Page
from django.http import HttpResponse

class PageUrl(Page):
    def returnPageUrl(self):
        page_url = """ i need the page url here """
        return page_url
Run Code Online (Sandbox Code Playgroud)

django wagtail

3
推荐指数
1
解决办法
3511
查看次数

如果字符串中出现特定子字符串,如何使用 python 返回子字符串?

我正在尝试使用 python 返回或打印一个非常长的字符串中搜索关键字之前出现的特定子字符串。

例如,如果我的字符串如下

string = "id:0, an apple a day keeps the doctor away, id=1, oranges are orange not red, id=3, fox jumps over the dog, id=4, fox ate grapes"
Run Code Online (Sandbox Code Playgroud)

如果搜索关键字是 apple 那么0(即 id)应该被打印

如果搜索关键字是橙色,1则应打印该关键字(即 id)

如果搜索关键字是fox,那么应该打印3 and 4(即id)

我期待示例中给出的关键字id 。就我而言,所有可搜索关键字都与示例字符串中的id相关联。

python

3
推荐指数
1
解决办法
44
查看次数