Search code examples
pythondjangodeploymentpythonanywhere

Django project on PythonAnywhere works but no functionality, although it does locally


I've just deployed my first site on PythonAnywhere, which is a Craigslist clone for which the search functionality works as expected locally but when deployed I can only access the homepage and then I get two different errors depending on the browser I try it on. Feel free to try it yourselves at http://matipule.eu.pythonanywhere.com/

This is Safari https://dpaste.com/3ZAJFC4PM

This is Chrome https://i.sstatic.net/7caWG.jpg

Chrome claims having an issue with the CSRF but I've included the token in the form as you can see below and then again, it actually works locally so I'm clueless.

This is the code where the token is in.

<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>

UPDATE What made it work was what the accepted answer suggested in UPDATE 2. Thanks!


Solution

  • in general, search requests are performed with GET method and you don't even need to mention method="GET" since it's the default method. Also you don't need the csrf_token in your form since it's a GET request.

    If you make those changes, the error should go but you'll get an other error :

    IntegrityError at /new_search/
    NOT NULL constraint failed: my_app_search.search
    

    i have no clue, but it seems to be related to your logic with database (sqlite3)

    if you share some code i could help you

    Update

    in 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.
    

    use GET instead POST since the search request is performed via GET method and you'll get ride off from the IntegrityError error. hope this solves your issue.

    Update 2

    besides the changes above, i deployed the app on heroku with little changes in settings.py

    ..
    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)
    
    ..
    

    and i run this command

    heroku addons:create heroku-postgresql:hobby-dev
    
    heroku run python manage.py migrate
    

    that's all what i've already done.