#I need help setting up setting up mongo Database to flask python

1 messages ยท Page 1 of 1 (latest)

wintry sleet
#

pymongo is not defined

#

anybody know why?

verbal imp
#

you don't have mongo.py in gmt folder

wintry sleet
#

its a package

#

i dont understand

#

@verbal imp thx for your attempt btw appreciate you msging me

verbal imp
#

from .. import means from folder up to current folder import something, if you want import something from installed packages don't put it in relative import

wintry sleet
#

ohh i see the confusion

#

so the thing is i didnt write this code, im working with a team but i am unable to locally run the html code iv made a local mongo database but after running that i recieved this bugs, i dont really know what to write as i am new to html

#

i am learning on the way

#

they are unable to help for reasons so i decided

verbal imp
#

thats python not html

wintry sleet
#

yeah ik but for that to work i was told this should be fixed

#

and idk what to import from

#

do you have any idea what directory is needed to import mail and mongo

#

or articles etc

verbal imp
#

show content of gmt/__inint__.py or if its empty what files are in gmt folder

wintry sleet
#

hold on il show you the content

#

files in gmt folder:

wintry sleet
verbal imp
#

show full error first including command you run

wintry sleet
#

ofc

#

is that sufficient?

#

or did i not send the right thing

verbal imp
#

its ok

#

show __init__.py

wintry sleet
verbal imp
#

you shouldn't add that view to ..

#

mongo is local variable to function create_app it cannot be imported in other file

wintry sleet
verbal imp
#

yes just ..

wintry sleet
#

in line 11 yes?

#

i

#

recieved an error

#

but views package is installed

verbal imp
#

it has to be from .. import

#

like it was from beginning

wintry sleet
#

yeah my bad

verbal imp
#

in file __init__.py at line 11 add mongo = PyMongo() and in line 20 change to mongo.init_app(app)

wintry sleet
#

im installing freepaser now

#

i have installed it with pip

#

but it still shows this

verbal imp
#

what thats old error you run twice after that without error

wintry sleet
#

odd it didnt, im a reload vs code

#

@verbal imp

#

nice that worked, can u help me take care of the other errors

verbal imp
#

what it is suppose to be that get_news? function then you don't have such in __init__.py

#

and judging by the name it don't belong to that file

wintry sleet
#

sorry?

verbal imp
#

in which file you have get_news function or variable defined?

wintry sleet
verbal imp
#

then why you changed it form this which was correct to just ..

#

.. means go up one directory if there is nothing more python will check __init__.py file, if your import is in different file you need to add that file name (without .py extension)

#

so ..news

wintry sleet
#

like this yeah?

verbal imp
#

yes

wintry sleet
#

AYYY

#

wait..

#

this is fine right?

verbal imp
#

yes

wintry sleet
#

THX SO MUCH BTW I REALLY APPRECIATE IT (You dont know how much this helps lmao)

#

i have one more enquiry if you dont mind

verbal imp
#

ask

wintry sleet
#

thx

#

so

#

with your help (thx again) i am running on the server however im receiving this problem

verbal imp
#

how did you open it?

wintry sleet
#

open live

#

locally

#

@verbal imp any idea?

verbal imp
#

something wrong with templates

wintry sleet
#

config template?

wintry sleet
verbal imp
#

idk which one

wintry sleet
#

Do you wanna live share I really want to start coding on my page as soon as possible

#

Ik I'm putting a lot of work on u but please ๐Ÿ™

verbal imp
#

its not working in my browser, and have no time

wintry sleet
#

damnn

#

ight thx alot for your help was really helpful

#

oh i just realised

#

Congrats on winning tmt challenge ๐ŸŽŠ

wintry sleet
#

sorry im i pushing it too much

verbal imp
#

if you don't have runtime errors then don't worry static analyser warnings for some project it require special configuration to work good

wintry sleet
#

but is it not still likley this might be the root cause

wintry sleet
verbal imp
#

check your browser console for errors and flask log

wintry sleet
#

check this out

verbal imp
#

some file names are wrongly generated 7B is encoded {

wintry sleet
#

Huh wdym

#

Ohh I see

verbal imp
#

template is not going through template engine but is served client directly

wintry sleet
#

How did u get that lmao explain so I can do the same

#

From this how would ik what to fix?

#

whats the explicit way you found out as in what line and how would i know how to fix it

verbal imp
#

whats in flask log

wintry sleet
#

il be honest idk where to go for that or how to set it up

verbal imp
#

its showing here

wintry sleet
#

Oh then whats this i found

verbal imp
#

hmm, compare this

#

with this

#

this are 2 different servers

wintry sleet
#

Oh am i connected to the wrong server?

wintry sleet
#

Should i change it to 55

verbal imp
#

lol don't use vscode live server, its only for static websites

wintry sleet
#

Ohh lmaoo

verbal imp
#

ctr click flask link in console

wintry sleet
#

Then what?

verbal imp
wintry sleet
#

Ah gotcha

#

Hold on

#

whats csrf

verbal imp
#

ask your team members how to config this

wintry sleet
#

get_config?

verbal imp
#

idk that flask_wtf lib so can't help

wintry sleet
#

Ah thats fair

#

I cant ask them tho for some reasons

wintry sleet
#

How did u get this

#

Im interested and want to learn

verbal imp
#

this is flask template engine syntax (by default jinja2), jinja replace this with generated html, so if you see this on page that means jinja didn't parse it

#

same is here %XX is some special char encoded to be safely used in URL, you can google them %7B is { and %7D is } and %20 is space so its {{ url_for(...) }} its jinja syntax

wintry sleet
#

Ohh i see

#

Then whats csrf about?

verbal imp
#

csrf is acronym for hackers attack method Cross-Site Request Forgery and in flask its module to secure your app against this attacks

wintry sleet
#

Ohh i see

#

After config that, would it fix the page

wintry sleet
#

Also should i leave mongo is not defined

wintry sleet
#

Oh you went offline: Thx for all of ur help so far, i hope u can answer my q when ur online again @verbal imp

verbal imp
wintry sleet
#

so how would i open page served by flask

wintry sleet
verbal imp
wintry sleet
#

Ohh i see and thats that

#

so how would i open page served by flask

verbal imp
#

you did opened it already (ctrl + click on link in console, or you can type that address manually in browser) as on same screen you can see get requests

#

to fix that error and other possible check if you have config.py filled with values if not you need to set all this environmental variables

wintry sleet
wintry sleet
verbal imp
#

all that are used by some code i don't know how much code is already implemented and how much of them are used

verbal imp
#

you can search for them in source or add them when you see errors so for now you need SECRET_KEY, MONGO_URI is overwritten in __init__.py its ok till you use local development db, it needs to be changed when website will be deployed

wintry sleet
#

Im confused

verbal imp
#

here is hardcoded instead loaded from config/environment variable

wintry sleet
verbal imp
#

this line app.config should not exists and next mongo.init_app(app) should be below load_configuration(app), what is now is ok till you run on your machine during developing process, but shouldn't ever reach deployment

#

this mongo URI should be saved as environmental variable or in config.py that should never be checked in repository and in production only environmental variable should be used no config.py

wintry sleet
#

If its ok then why do i get the errors

verbal imp
#

i say only MONGO_URI currently is ok and it don't give you error

#

SECRET_KEY which gives you error you need to set

wintry sleet
#

Ohh i see so it shouldnt reach deployment

#

are you talking about these fields

wintry sleet
verbal imp
#

so they give you clear instruction to follow and you didn't

wintry sleet
#

No i just found it PepeSad

#

This is for configuration right?

#

And that will fix the errors right??

verbal imp
#

if you follow all of them and don't make mistake then yes it should work

wintry sleet
#

Yeah thx, so that would then allow the pages to be dynamic generated content

verbal imp
#

if you don't use vscode live preview but flask server then yes

wintry sleet
#

I should use what because i dont get the link anymore

#

It onlr runs when i use vscode live preview

verbal imp
#

you need to see this

#

if you don't it will not work

#

and trying to use live preview is completely pointless

wintry sleet
#

Thats the thing i dont see that anymore i just see thr flask logs

verbal imp
#

if you see flask logs thats ok

#

this means server is running and request comes to flask

#

you can use ctrl-c to stop it and if you run again you'll see again whats on above image

wintry sleet
#

Ah ic after pressing ctrl c il get the link again and i just go to that right?

verbal imp
#

yes, some changes in code can be seen when flask is running, for most you need restart flask server

wintry sleet
#

Flask server? Do u mean the link

#

Just reload it ?

verbal imp
#

no thats page on client side

verbal imp
wintry sleet
#

Ohh i see by pressing ctrl c i am restartung the flask server?

verbal imp
#

pressing ctr -c and staring server again, as ctrl-c only stops server

wintry sleet
#

Ahh yeah i get it

#

So

#

After i configure it and go to a html page it should be showing dynamic content right?

verbal imp
#

yes if flask starts without errors

wintry sleet
#

Ayy nice so only configuration is left?

#

And also in what instance would i use the go live rather than the flask server

verbal imp
#

if you write static page only

#

where static means everything happens on client side there is no need to talk to server except download files that are stored on server (server don't generate nothing in runtime)

wintry sleet
#

So u wouldnt run a home page with that as it is dynamic

verbal imp
#

its not single page from whole website that needs to be static or dynamic its whole website that need to be static

wintry sleet
#

Hold on but am i not running static cus it says u would use html and css

#

How would u know the website im working on is dynamic is it because of flask

#

And syntax

verbal imp
#

you always use html and css as that whats needed for browser to display page

wintry sleet
#

Yeah make sense

verbal imp
#

if you write code that will run on server side thats when website is dynamic, and yes flask is framework to run on server, browser can't run python code

wintry sleet
#

Ahhhh

#

Honeslt i appreciate u being very patient with me and thx alot for ur help

verbal imp
#

yw

wintry sleet
#

Have a nice day tovisokbro

wintry sleet
#

@verbal imp hey Yan ๐Ÿ˜… need help

#

up to this iv followed instruction (up to run index.py) but when now when i run index.py it says "No module named 'config" so i dowloaded the module from cmd now it shows "No module named 'config.template'"

#

sorry to disturb you again, hope you can help

verbal imp
#

what cmd you used?

wintry sleet
#

i used cd (the directory) mkdir instance

#

which created a folder

#

i then moved config templates inside instance and renamed it to

#

as well as doing pip install -r requirements.txt

verbal imp
#

show full current error

wintry sleet
#

sure

#

just to be clear

#

here i am

wintry sleet
#

any ideas?

verbal imp
#

show file tree from vscode

wintry sleet
verbal imp
#

dont use windows explorer to rename files or turn on show file extension

#

it need to be named config.py not config.py.py

#

right click in vscode and choose rename

wintry sleet
#

done but still shows

#

ohh becuase i renamed config.template to config.py it does not recognise where to from

#

is that why?

verbal imp
#

yes, but it shouldn't work like that, show readme above that renaming config.py

wintry sleet
verbal imp
#

this line is not orginaly in that file,
why did you added it? you should read config from app.config dictionary not from file and why do you need MONGO_URI in that file you shouldn't create another connection to data base but use already existing

wintry sleet
#

so what should it be instead? what

verbal imp
#

from .. import mongo as it was orginaly if you want query db use this mongo variable look how its done in other functions in that file

wintry sleet
#

should i take this out completely then ?

wintry sleet
verbal imp
#

in index.py yes this line is not needed, same in __init__.py modify config.py and change it there

wintry sleet
#

what line and what file

verbal imp
#

probably you should delete it all clone from beginning follow the steps in readme to install and configure then run without touching any code (except config.py) if website is running correctly then do your changes to code

wintry sleet
#

alr let me try that

#

should i just copy my current code in config py and place it in the new clone

#

@verbal imp

wintry sleet
#

Right back where we started :/

verbal imp
wintry sleet
#

Yeah

verbal imp
#

add in __init__.py in line 76 (move mongo.inint_app(app) one line down) print(app.config["MONGO_URI"])

wintry sleet
#

like this?

verbal imp
#

yes

#

run and show output

wintry sleet
#

should i run index

verbal imp
#

yes

wintry sleet
verbal imp
#

show more

#

everything from your command down

wintry sleet
verbal imp
wintry sleet
#

well i havnt did configuration/ the instruction from readme yet ๐Ÿ˜…

#

ill do it now lmaoo

#

OH MYY , im actually so sorry for my stupidity i could avoided wasting u time for hours.im really sorry

verbal imp
#

you learnt something, remember if you clone some project first make it run before you do any changes, and good project will have instructions how to set it up

#

if its working remove that print you added

wintry sleet
#

yeah will do, im so sorry thx alot for coping with me and being so patient