Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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
Pythonywhere上的Django项目可以工作,但没有功能,尽管它在本地工作_Python_Django_Deployment_Pythonanywhere - Fatal编程技术网

Pythonywhere上的Django项目可以工作,但没有功能,尽管它在本地工作

Pythonywhere上的Django项目可以工作,但没有功能,尽管它在本地工作,python,django,deployment,pythonanywhere,Python,Django,Deployment,Pythonanywhere,我刚刚在Pythonywhere上部署了我的第一个网站,这是一个Craigslist克隆版,搜索功能在本地可以正常工作,但部署后我只能访问主页,然后根据我尝试使用的浏览器,会出现两个不同的错误。你可以自己在家里试试 这里是Safari 这是Chrome Chrome声称CSRF有问题,但我已经在表单中包含了令牌,正如你在下面看到的,然后再次强调,它实际上在本地工作,所以我不知道 这是令牌所在的代码 <form action="{% url 'new_search' %}&qu

我刚刚在Pythonywhere上部署了我的第一个网站,这是一个Craigslist克隆版,搜索功能在本地可以正常工作,但部署后我只能访问主页,然后根据我尝试使用的浏览器,会出现两个不同的错误。你可以自己在家里试试

这里是Safari

这是Chrome

Chrome声称CSRF有问题,但我已经在表单中包含了令牌,正如你在下面看到的,然后再次强调,它实际上在本地工作,所以我不知道

这是令牌所在的代码

<form action="{% url 'new_search' %}" method="post">
     {% csrf_token %}
     <input type="text" name="search" placeholder="search">
     <button class="btn waves-effect waves-light" type="submit" name="action">Submit
         <i class="material-icons right">send</i>
     </button>
 </form>

{%csrf_令牌%}
提交
发送
更新
是什么让它起作用的是更新2中被接受的答案所建议的。谢谢

通常,搜索请求是使用
GET
方法执行的,您甚至不需要提及
method=“GET”
,因为它是默认方法。另外,您不需要在
表单中使用
csrf\u令牌
,因为这是一个
GET
请求

如果您进行了这些更改,错误应该会消失,但您将得到另一个错误:

IntegrityError at /new_search/
NOT NULL constraint failed: my_app_search.search
我不知道,但它似乎与您的数据库逻辑(sqlite3)有关

如果你分享一些代码,我可以帮你

更新 在
views.py中

def new_search(request):
    # search = request.POST.get('search')
    search = request.GET.get('search')  # the value should be Not NULL and the 'IntegrityError' should go.
..
ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com']  # HERE
..

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# HERE added these lines to override the default database config with postgress settings
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

..
使用
GET
代替
POST
,因为搜索请求是通过
GET
方法执行的,您将从
IntegrityError
错误中解脱出来。希望这能解决你的问题

更新2 除了上面的更改之外,我在
heroku
上部署了应用程序,在
settings.py

def new_search(request):
    # search = request.POST.get('search')
    search = request.GET.get('search')  # the value should be Not NULL and the 'IntegrityError' should go.
..
ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com']  # HERE
..

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# HERE added these lines to override the default database config with postgress settings
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

..
我运行这个命令

heroku addons:create heroku-postgresql:hobby-dev

heroku run python manage.py migrate

我已经做了这些。

哦,很高兴知道。我仍然在开始我的旅程,所以我通过一个教程构建了这个,这就是为什么GET方法与csrf_令牌一起使用,对他们来说它是这样工作的,这就是为什么我感到困惑。该项目的所有代码都可以在这里找到@MatíasPuletti我更新了我的答案。让我知道它是否有效我已经实现了你的建议,同时删除了csfr_令牌,但不幸的是,我仍然发现CSRF令牌丢失或错误。现在,这个错误在Safari和Chrome中都出现了。我不懂。PythonyWhere支持人员建议,正如您所建议的,删除令牌并通过其仪表板启用强制HTTPS,但这也不起作用。这就是我现在得到的,太棒了!我只需要删除它,不指定任何GET,对吗?