Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Django+;JS!=CSRF_Javascript_Python 3.x_Django - Fatal编程技术网

Javascript Django+;JS!=CSRF

Javascript Django+;JS!=CSRF,javascript,python-3.x,django,Javascript,Python 3.x,Django,我发现,如果我在django项目中将html与js链接,那么CSRF验证就会失败。请求被中止。如果我不把html和js链接在一起,它会工作得很好。那么我该如何解决这个问题呢?下面是views.py和style.js文件: 这个网站是关于天气的。如果我按下按钮搜索天气与未链接的js它的作品罚款 views.py def index(request): owm = pyowm.OWM(":)") mgr = owm.weather_manager() i

我发现,如果我在django项目中将html与js链接,那么CSRF验证就会失败。请求被中止。如果我不把html和js链接在一起,它会工作得很好。那么我该如何解决这个问题呢?下面是views.py和style.js文件: 这个网站是关于天气的。如果我按下按钮搜索天气与未链接的js它的作品罚款

views.py

def index(request):
    owm = pyowm.OWM(":)")
    mgr = owm.weather_manager()

    if(request.method == "POST"):
        form = CityForm(request.POST)
        form.save()

    form = CityForm()
    city = City.objects.last()
    
    result = get_todays_weather(mgr, city.name)
    forecast_hourly = get_todays_forecast(mgr, city.name) 

    context = {
        "info": result,
        "forecast_hourly": forecast_hourly,
        "form": form
    }

    return render(request, "index.html", context) 
style.js


var check = function () { 
    var hours = new Date().getHours();
    hours = 3
    if (hours < 5  )
    { 
      document.getElementById("header_id").style.background = "linear-gradient(to bottom, #692dad, #442aa3)";
      document.getElementById("brand_id").style.color = "#f9fbfc";
      document.getElementById("body_id").style.background = "#8f7cd6";
      document.getElementById("brand_id").style.color = "#f9fbfc";

      var elements = document.getElementsByClassName("nav-link");
      for(var i = 0; i < elements.length; i++)
      {
          if(elements[i].className != "nav-link active")
          {
            elements[i].style.color = "#f9fbfc";
          } 
      }
      document.getElementById("search_btn").style.color = "#f9fbfc"
      document.getElementById("second_card_id").style.background  = "linear-gradient(to bottom, #692dad, #442aa3)";
      var cards = document.getElementsByName("card");
      for(var i = 0; i < cards.length; i++)
      { 
          cards[i].style.background = "linear-gradient(  white 25%, #692dad 50%, white 75% )";
      }
      document.getElementById("card_title_id").style.color = "#f9fbfc";
      document.getElementById("footer_id").style.background = "linear-gradient(to bottom, #692dad, #442aa3)";

    }
    else if (hours < 8  && hours > 5)
    {
        document.getElementById("header_id").style.background = "linear-gradient(to top, #e2e498, #4718f0)";
        document.getElementById("search_btn").style.color = "#f9fbfc"
    }
    else
    {
        document.getElementById("header_id").style.background = "linear-gradient(to top, #ffffff, #C2D0FA)";
        document.getElementById("search_btn").style.color = "#FBFBFB";
        document.getElementById("search_btn").style.background = "#2E50B0";
        var cards = document.getElementsByName("card");
        for(var i = 0; i < cards.length; i++)
        {
            //cards[i].style.background = "#D5B2EB"; 
            cards[i].style.background = "linear-gradient(  white 25%, #adc1fd 50%, white 75% )";
        }
    
    }
  }
  

check();

var check=函数(){
var hours=新日期().getHours();
小时=3
如果(小时数<5)
{ 
document.getElementById(“header_id”).style.background=“线性渐变(到底部,#692dad,#442aa3)”;
document.getElementById(“品牌id”).style.color=“#f9fbfc”;
document.getElementById(“body_id”).style.background=“#8f7cd6”;
document.getElementById(“品牌id”).style.color=“#f9fbfc”;
var elements=document.getElementsByClassName(“导航链接”);
对于(var i=0;i5小时)
{
document.getElementById(“header_id”).style.background=“线性渐变(到顶部,#e2e498,#4718f0)”;
document.getElementById(“search_btn”).style.color=“#f9fbfc”
}
其他的
{
document.getElementById(“header_id”).style.background=“线性渐变(到顶部,#ffffff,#C2D0FA)”;
document.getElementById(“search_btn”).style.color=“#fbfb”;
document.getElementById(“search_btn”).style.background=“#2E50B0”;
var cards=document.getElementsByName(“卡片”);
对于(变量i=0;i
django文档中有一部分描述了如何从javascript获取CSRF令牌。我在一些使用Django REST框架从js查询api视图的教程中看到了这一点。也许是个好的开始


这有帮助吗?还有,你所说的“链接html和那个js”到底是什么意思?你说的是
标签吗?