0

I'm trying to send a GET request and a POST requests. Both http requests gave me a 404 error code. I do have my tables already up on PGAdmin and my database (I had to manaully create the tables because, the flask migrations weren't working. Chatgpt had said make sure your server url is correct even though I made sure so many times. I'm running this all on a docker container (the docker container has 2 containers). I even made sure that was correct. Idk what I am doing wrong.

This is my init.py file

import os
from flask_migrate import Migrate
from flask import Flask
from flask_sqlalchemy import SQLAlchemy




def create_app(test_config=None): 
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        SQLALCHEMY_DATABASE_URI = 'postgresql://postgres@localhost:5432/amazonclone',
        SQLALCHEMY_TRACK_MODIFICATIONS=False,
        SQLALCHEMY_ECHO=True

    )
    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    db = SQLAlchemy(app)
    
    migrate = Migrate(app, db)

    return app



# this is my wspi.py file


from src import create_app


app = create_app()

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)`


# this is my route file for my api (i'm only gonna post part of the file because its too long) 

from flash import Flask, jsonify, request, abort
from bookModel import Book, BookSchema
from clothingModel import Clothing, ClothingSchema
from movieModel import Movie, MovieSchema
from home_garden import HomeAndGarden, HomeAndGardenSchema
from electronic import Electronic, ElectronicSchema
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres@localhost:5432/amazonclone'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)

# Book routes
@app.route('/books', methods=['POST'])
def add_book():
    # Check if the requst body contains all required keys
    if not all(key in request.json for key in ['genre', 'author', 'style', 'name']):
        abort(400, 'Missing required keys in Json payload')

    # Retrieve the data from the request body
    genre = request.json['genre']
    author = request.json['author']
    style = request.json['style']
    name = request.json['name']


    # Create a new book object and add it to the database
    my_books = Book(genre, author, style, name)
    db.session.add(my_books)
    db.session.commit()

    # Serialize and return the new book object
    return jsonify(Book.dump(my_books)), 201

# the code above is just for one of my files, the route file is much longer than that. 
Babit56
  • 1
  • 1
  • first thing first sir, why migrations did not work ? please check that and share some findings :) – jmvcollaborator Jun 19 '23 at 22:57
  • okay nevermind the migrations work, but I already manually made the online pgadmin tables. I think migrating the models on there own would be the same result. I am having issues running http requests. – Babit56 Jun 20 '23 at 00:32
  • 1
    Please add your docker configuration or the `docker run` command that you used – Pratik Thakare Jun 20 '23 at 03:13
  • Oh I didn't have to use docker run commands. My docker desktop was running the containers fine on their own. – Babit56 Jun 20 '23 at 18:07

0 Answers0