相关疑难解决方法(0)

设置cookie并使用JavaScript获取cookie

我正在尝试根据我在HTML中选择的CSS文件来设置cookie.我有一个包含选项列表的表单,以及不同的CSS文件作为值.当我选择一个文件时,它应该保存到cookie大约一个星期.下次打开HTML文件时,它应该是您选择的上一个文件.

JavaScript代码:

function cssLayout() {
    document.getElementById("css").href = this.value;
}


function setCookie(){
    var date = new Date("Februari 10, 2013");
    var dateString = date.toGMTString();
    var cookieString = "Css=document.getElementById("css").href" + dateString;
    document.cookie = cookieString;
}

function getCookie(){
    alert(document.cookie);
}
Run Code Online (Sandbox Code Playgroud)

HTML代码:

<form>
    Select your css layout:<br>
    <select id="myList">
        <option value="style-1.css">CSS1</option>
        <option value="style-2.css">CSS2</option>  
        <option value="style-3.css">CSS3</option>
        <option value="style-4.css">CSS4</option>
    </select>
</form>
Run Code Online (Sandbox Code Playgroud)

html javascript css cookies

486
推荐指数
4
解决办法
95万
查看次数

CSRF令牌丢失或不正确

这里是DJango的初学者,我一直试图解决这个问题很长一段时间.我的中间件类中有'django.middleware.csrf.CsrfViewMiddleware',我的帖子中有令牌.

继承我的代码,我做错了什么?

from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from chartsey.authentication.forms import RegistrationForm
from django.template import RequestContext
from django.core.context_processors import csrf

def register(request):

    if request.method == 'POST':
        c = RequestContext(request.POST, {})
        form = RegistrationForm(c)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/")
    else:
        form = RegistrationForm()

    return render_to_response("register.html",  {'form': form,  }, )
Run Code Online (Sandbox Code Playgroud)

这是我的模板:

{% block content %}

    <h1>Register</h1>
    <form action="" method="POST"> {% csrf_token %}
        {{ form.as_p }}
    <input type="submit" value="Submit">
    </form>

{% endblock %}
Run Code Online (Sandbox Code Playgroud)

python django csrf django-forms

19
推荐指数
2
解决办法
6万
查看次数

Django CSRF cookie未正确设置

更新7-18:

这是我的代理服务器的nginx配置:

server {
    listen 80;
    server_name blah.com; # the blah is intentional

    access_log /home/cheng/logs/access.log;     
    error_log /home/cheng/logs/error.log;       

    location / {
        proxy_pass http://127.0.0.1:8001;         
    }

    location /static {
        alias /home/cheng/diandi/staticfiles;  
    }

    location /images {
        alias /home/cheng/diandi/images;
    }

    client_max_body_size 10M;
}
Run Code Online (Sandbox Code Playgroud)

这是nginx.conf:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings …
Run Code Online (Sandbox Code Playgroud)

python django cookies csrf

18
推荐指数
1
解决办法
3884
查看次数

CSRF cookie未设置django ...验证失败

AoA我是Django的新手,我试图从POST获取数据,但是没有设置错误CSRF cookie,我尝试了很多通过谷歌在google和stackoverflow上找到解决方案,但是失败了

这是代码

views.py

    from django.http import HttpResponse
    from django.template.loader import get_template
    from django.template import Context
    from django.template import RequestContext
    from django.core.context_processors import csrf
    from django.shortcuts import render_to_response

def search_Post(request):
    if request.method == 'POST':
            c = {}
        c.update(csrf(request))
        # ... view code here
                return render_to_response("search.html", c)

def search_Page(request):
    name='Awais you have visited my website :P'
    t = get_template('search.html')
    html = t.render(Context({'name':name}))
    return HttpResponse(html)
Run Code Online (Sandbox Code Playgroud)

HTML文件

<p>
          {{ name }}
            <form method="POST" action="/save/">
              {% csrf_token %}
              <textarea name="content" rows="20" cols="60">{{content}}</textarea><br>
              <input type="submit" …
Run Code Online (Sandbox Code Playgroud)

python django cookies

4
推荐指数
2
解决办法
2万
查看次数

使用chrome POSTMAN测试HTTP POST时,在Django中不起作用

我用 Django 1.9.7 & Python 3.5

我实现了创建用户机制并尝试使用POSTMAN(chrome应用程序)进行测试,但是它不起作用,并且显示如下内容:

Forbidden (CSRF cookie not set.): /timeline/user/create/
Run Code Online (Sandbox Code Playgroud)

这是代码:

urls.py

from django.conf.urls import url
From. import views

app_name = 'timeline'
urlpatterns = [
    # ex) /
    url(r'^$', views.timeline_view, name='timeline_view'),

    # ex) /user/create
    url(r'^user/(?P<method>create)/$', views.user_view, name='user_view'),
]
Run Code Online (Sandbox Code Playgroud)

views.py

from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, HttpResponse

from timeline.models import *


def timeline_view(request):
    return HttpResponse('hello world')


def user_view(request, method):
    if method == 'create' and request.method == 'POST':
        print("hi")
        username = request.POST.get('username')
        username = request.POST.get('username') …
Run Code Online (Sandbox Code Playgroud)

python django postman

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

标签 统计

django ×4

python ×4

cookies ×3

csrf ×2

css ×1

django-forms ×1

html ×1

javascript ×1

postman ×1