写了个很简单的web,其他什么都没有,一直想不明白为什么csrf_token报错,开发环境没有问题的.
找到原因了,感谢博主:https://blog.csdn.net/weixin_37770279/article/details/124480045
{% block content %}
<div>
{% for datum in data %}
<text>{{ datum }}</text>
{% endfor %}
</div>
<form action="{% url '200' %}" method="post">
{% csrf_token %}
<input type="text" name="name" id="name">
<button type="submit" id="sumbit">点击它</button>
</form>
{% endblock content %}
def index(request):
if request.method == "GET":
logging.info('index')
return render(request, 'myapp/index.html', context={'data': '123'})
def index2(request):
if request.method == "POST":
logging.info('this is post')
return HttpResponse(status=200, content='this is post')
from django.urls import path
from myapp import views
urlpatterns = [
path('index/',views.index,name='index'),
path('200/', views.index2, name='200'),
]