a year ago
I have Django, React application live on railway. I'm using Hobby plan.
whenever I click through pagination buttons server is taking up to a minute to respond. I can see in the network panel that request has been sent and it takes 10 - 60 second for the response to come in.
on the localhost everything is instant. I don't know what's causing it.
Full project code : https://github.com/AlexShonia/e-commerce
pagination code on django:
@api_view(["GET"])
def getProducts(request):
query = request.query_params.get("keyword")
if query == "null" or query == None:
query = ""
products = Product.objects.filter(name__icontains=query)
page = request.query_params.get("page")
paginator = Paginator(products, 4)
try:
products = paginator.page(page)
except PageNotAnInteger:
products = paginator.page(1)
except EmptyPage:
products = paginator.page(paginator.num_pages)
serializer = ProductSerializer(products, many=True)
return Response(
{"products": serializer.data, "page": page, "pages": paginator.num_pages}
)
0 Replies
a year ago
how are you serving your frontend?
npm run build and python manage.py collectstatic
a year ago
this is never a good idea
a year ago
please separate this repo out into two folders -
backend
manage.py
...
frontend
package.json
...
if I did this I'm guessing I'd have to change how I'm specifying staticfiles path here:
https://github.com/AlexShonia/e-commerce/blob/174204655011dc89bf06e2f3f34858ea2f5576e7/backend/backend/settings.py#L169
would there be anything else to change?
a year ago
there is many things to change, serving the frontend from a django backend is far from ideal
if I didn't serve frontend from django backend would that mean I'd have to host frontend separately from backend? I'm quite new to this so I don't know the best practices. where would I find more information about this?
a year ago
yes your frontend will be served by another railway service, that is standard practice
a year ago
finding more information about this means just following along while I guide you
a year ago
awesome, that means also removing anything from the backend code that is meant to serve the frontend
I'm guessing staticfiles directory and the code connected to it will be removed
a year ago
yeah all the frontend assets will be served by the frontend service
what about images that users can upload? can backend store them in the public folder on the frontend?
a year ago
yeah that's backend stuff
a year ago
well that's not ideal
a year ago
haha
this is basically my portfolio website and maybe they thought it was actualy e-comemrce website and I didnt meet rules or something is probably what happened
a year ago
sounds good, feel free to ping me when you have that sorted!
a year ago
no prob
Okay I'm back. idk what that was but after some time I just gained access again. weird.
anyway I spearated the repositories and deleted static files and some code connected to it
https://github.com/AlexShonia/e-commerce/commits/main/
a year ago
please remove all code from the backend that serves the frontend
like this -
a year ago
make sure you remove the frontend's dist folder from github and add it to your .gitignore
I'm guessing this is how I'd deploy front and back from same repository seperately
a year ago
well hold on there
a year ago
can i ask you to switch to postgres instead of sqlite?
a year ago
yes it really would
okay that should be it, I made a postgres database on railway and connected it with settings.py:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("PGDATABASE"),
"USER" : os.environ.get("PGUSER"),
"PASSWORD" : os.environ.get("PGPASSWORD"),
"HOST" : os.environ.get("PGHOST"),
"PORT" : os.environ.get("PGPORT"),
}
}
a year ago
looks good to me, do you have all those variables set in your service variables for the backend service?
a year ago
click the eye icon on the backend's service variables to make sure they show correctly
oh yeah some are empty for some reason even though I copied raw text I'll fix that.
also Im getting this errors on frontend i the logs. I'll look into that
a year ago
please slow down, there are changes we need to make still, you wont be able to just give railway your repo just yet
a year ago
these are what you want to use for your app's database variables, not what you currently have
PGDATABASE=${{Postgres.PGDATABASE}}
PGUSER=${{Postgres.PGUSER}}
PGPASSWORD=${{Postgres.PGPASSWORD}}
PGHOST=${{Postgres.PGHOST}}
PGPORT=${{Postgres.PGPORT}}
a year ago
please use the variables i provided
a year ago
is this the raw editor of the postgres service?
a year ago
why do you have every variable in there that the postgres service has?
a year ago
yes
a year ago
okay now make sure they display correctly
a year ago
you would need to save the changes with alt + shift + enter
a year ago
okay can i see a screenshot of your railway project now please
a year ago
yeah thats good
a year ago
okay code changes time
a year ago
lmao
a year ago
in your backend, delete the Procfile and replace it with this railway.json file
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "python manage.py migrate && gunicorn backend.wsgi --log-file -",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
a year ago
and the root directory should be set to /backend
a year ago
okay now let me know if the backend fails
SECRET_KEY = os.environ.get("SECRET_KEY")
I get secret key from environment variables and I dont have it set anymore
a year ago
yeah just set it as a service variable
a year ago
ive updated this
a year ago
awsome
a year ago
on to the frontend
a year ago
copy the nixpacks.toml and Caddyfile from this repo into your frontend folder.
you will want the root directory on the frontend service set to /frontend
a year ago
and let me know if something with the frontend fails
a year ago
okay now you will have to update your frontend code to call the backend domain
a year ago
awsome
a year ago
nothing is too slow?
a year ago
sounds good
hmm the problem remains still, must be something with my code then. regardless it was great to improve the architecture and conect to postgres
a year ago
do you have some middleware that's slowing things down?
not that I know of. only the pagination is the problem it seems. registration and login is pretty fast as well as other operations
a year ago
well to be honest, I wasn't fully expecting these changes to fix anything
a year ago
but it at least does mean it's an issue with the code and not the structure, so it was definitely worth while to do this
a year ago
happy to help where I can, and definitely let me know when you find out what code is responsible for the slow down
I think I found out what the problem is lol. I just found out the Server is deplyed in US West Oregon and I'm in Georgia (country) so the ping should be very high. I wonder how the site is doing in US.
a year ago
how long is too slow again? you said 60 seconds right?
sometime it's quite fast. 1-3 seconds. and sometimes it's like 30 seconds and worker times out.
a year ago
is there a page link you could send me so i could test? im not us-west but i am far closer than you are
when I'm in localhost and I make connection to local postgres db the responses are very fast (under a second) so thats why I'm thinking it's not the code but where the site is hosted.
a year ago
just loading the content for that page took ~17 seconds, distance is not the main issue you are facing
a year ago
indeed
from what I've tested the problematic line is this in the django:
if(query):
products = Product.objects.filter(name__icontains=query).order_by("_id")
else:
products = Product.objects.all().order_by("_id")
I tested this by printing something first and then printing products and products got printed much later
a year ago
interesting
After countless hours of researching I found out that the problem appears only on firefox. I tested edge and chrome and they are all fine. yes the response sometimes takes 1-3 seconds but it's only expected as server is so far away. and nothing compared to 30+ seconds. and btw it's not actually 30+ seconds it's just that gunicorn times out after 30 seconds and when I set timeout to 5 minutes the problematic responses took all 5minutes.
now this problem doesn't bother me as much now because most people use chrome anyway and this is just a portfolio website. but another problem I found is that backend django is in debug mode still even though I specified Debug= False.
wait nvm, I thought debug mode meant that I wouldn't even be able to access /admin page
a year ago
I was on chrome
that was a different problem. I think I fixed that by usign collectstatic in start configurations? idk or maybe because I changed debug to false. but the 30+ sec response doesn't happen on chrome. and the load times are good for me
a year ago
load times are good for me now too
a year ago
job done
a year ago
please open your own help thread