#web-development

2 messages · Page 102 of 1

blissful slate
#

@mortal mango

(venv) $ deactivate
$ rm -rf venv
$ python3 -mvenv venv
$ . venv/bin/activate
(venv) $ echo django==3.1.2 > requirements.txt
(venv) $ pip install -r requirements.txt
wanton ridge
#

@blissful slate i fixed it bro

blissful slate
#

nice one @wanton ridge how'd you do it

wanton ridge
#

my host server was wrong lol, it's 127.0.0.1

blissful slate
#

i thought it might be that. 👍

mortal mango
#

@devout coral it's showing 3.1.2 now

#
import os
import sys

def main():
    # """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'exo_api.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()
#

this is the code with the error

#

it's pointing to execute_from_command_line and from exc

devout coral
#

What are you running when the error pops up?

mortal mango
#

wdym

devout coral
#

Like why is the error popping up?

#

Or when is it popping up

mortal mango
#

it pops up when I do python manage.py runserver

devout coral
#

Can you post the error again please.

mortal mango
devout coral
#
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'exo_api.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

Here just delete everything inside of manage.py and paste this. It looks like you might have added a space or something somewhere

mortal mango
#

oh ok

#

no module named psycopg2

devout coral
#

in your venv do a pip install for that module

#

@mortal mango pip install psycopg2

mortal mango
#

ok I did

devout coral
#

now try to run the server again

mortal mango
#

cannot import name views

#

from . import views

blissful slate
#

@mortal mango I reproduced your issue...

#

if I use #!/usr/bin/env python3.8 at the top of the script it fails; if run python3.8 test.sh it fails. but if I run python test.sh it works for me. not sure why. This is all using pyenv installed pythons

mortal mango
#

oh so what do I do?

blissful slate
#

if I use #!python at the top then it works. 🤷‍♂️

mortal mango
#

wdym #!python

blissful slate
#

how are you running the script

mortal mango
#

VPS Ubuntu 18.04

blissful slate
#

and you have a text file called test.py or whatever and the first line is #!python

mortal mango
#

I have a file called tests.py that was automatically installed

#

that one?

#

or do I create a new file and put #!python in it

#

@blissful slate

blissful slate
#

well what are you typing in to make the script go

mortal mango
#

or where do I import it from

#

because I'm getting an error there

#

@blissful slate @devout coral do you guys know?

blissful slate
#

oh i thought you're running a script here. i dont know django very well

devout coral
#

Well the error you are getting now is what? Import problems in views?

mortal mango
#

I did from exo_api import views

#

and it says I have an error there

#

I have 2 folders named exo_api so i'm not sure if that's the problem or not

devout coral
#

Where is the file located that you are trying to import from?

#

Send me a screenshot of your directory tree please

mortal mango
#

I have the urls.py file inside the exo_api inside the exo_api

#

the one that's selected right now

#

should I rename it?

devout coral
#

No wait.

#

Could you send the entire tree. With the manage.py file and the root folder for the project

mortal mango
#

that's a screenshot I took yesterday so I don't have the folder with the models folder

devout coral
#

Ok, so where is the folder that has the models? Inside of the exo_api folder? Or in the root with manage.py

mortal mango
devout coral
#

Ok, now the second picture you sent. What folder is that?

mortal mango
#

and the urls.py file is in the exo_api folder

#

the second picture is inside the exo_api folder inside the exo_api folder

#

so the exo_api folder that's seen in the third picture

#

should I just rename the exo_api folder that's in the third picture

devout coral
#

No don’t do that. That will break it.

#

Give me like 5 minutes. I need to hop on a conference call

mortal mango
#

oh ok, take your time

devout coral
#

so your root folder is called exo_api correct?

mortal mango
#

yeah the root folder holds the manage.py file right?

devout coral
#

Yes

mortal mango
#

yeah it's called exo_api

devout coral
#

Then inside that folder you have another folder with the same name right?

mortal mango
devout coral
#

These are the only files you need in there. It looks like some of the contents of the venv are in yours and your gitignore delete all of that other stuff

mortal mango
#

yeah those are the ones I have

#

I have a pycache folder inside that folder

#

should I keep that or delete it?

devout coral
#

Keep it. Granted if you delete it it will pop up. Those contain stuff that python packages use

mortal mango
#

oh ok

#

then I have everything that I need

devout coral
#

Cool

mortal mango
#

do you know what's causing the error?

devout coral
#

The error you are getting is inside your urls.py file right?

mortal mango
#

it's supposed to be from exo_api import views right?

#

yeah

devout coral
#

@mortal mango No, send me the code you have in urls.py

mortal mango
#
from django.contrib import admin
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from exo_api import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('exo/', views.exoList.as_view()),
]
devout coral
#

it should be from users import views

mortal mango
#

oh ok

devout coral
#
from django.contrib import admin
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from users import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('exo/', views.exoList.as_view()),
]
mortal mango
#

I'm getting this error now

devout coral
#

in settings.py send me what the variable INSTALLED_APPS contains

#

@mortal mango ?

mortal mango
#

oh ok

#

let me send them

#
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
]
#

do I need to add exo_api? or users to it?

devout coral
#

Yes you need to add your custom apps in there

mortal mango
#

so both?

#

or just exo_api

devout coral
#

'users.apps.UsersConfig'

mortal mango
#

oh ok

devout coral
#

and whatever other apps you have

mortal mango
#

I only have 1

next tree
#

so I understand the purpose of jwt tokens and using them for sessions, but unclear about the purpose of refresh tokens

#

are refresh tokens supposed to be used for refreshing the original session token in case the user's initiated action happened right before expiration?

#

or is there a different reason?

devout coral
#

@mortal mango Did it work?

mortal mango
#

it says I have to replace it with WebappConfig so I'll do that

devout coral
#

sure

mortal mango
#

same error for some reason

devout coral
#

Which error?

mortal mango
devout coral
#

Ok you still need to add that users app

#

It is not there

mortal mango
#

wdym

devout coral
#

Your users app is not registered in installed apps

mortal mango
devout coral
#

no

#

put it in installed apps

mortal mango
#
from django.apps import AppConfig


class WebappConfig(AppConfig):
    name = 'exo_api'
```this is what I have in apps.py
#

oh ok

devout coral
#

Oh

#

why is your app called exo_api?

#

I thought it was called users....

mortal mango
#

I'm so dumb

#

Idk even when I put that there

#

lol

devout coral
#

So you got it?

mortal mango
#

it says database 'exo_api' does not exist

#

and I tried to make migrations but it brings up another error

devout coral
#

How did you set this project up? Seems like it was not set up properly

mortal mango
#

I uploaded the files from my computer. Do I need to actually startproject?

devout coral
#

It works on your computer?

mortal mango
#

no I never tested it

devout coral
#

...

mortal mango
#

is there a way I can set the project up correctly?

devout coral
#

Yes. Get off the server. Start from the beginning on your computer and do not push the files until AFTER you have tested it.

mortal mango
#

k

#

in the database section in settings.py, what do I put for the name part?

#

would I put users?

#

@devout coral

#

how do I reset my postgresql password?

devout coral
#

@mortal mango What do you mean?

mortal mango
#

I can't seem to login to the database with the code

#

it says password authentication failed for user

devout coral
#

On your test environment I suggest you use sqllite

mortal mango
#

oh ok

#

I haven't created a database with sqlite

devout coral
#

When you run migrations it will do it

mortal mango
#

oh ok

devout coral
#
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

That should be somewhere in your settings.py

mortal mango
#
django.db.migrations.exceptions.NodeNotFoundError: Migration users.0002_auto_20201019_1816 dependencies reference nonexistent parent node ('webapp', '0001_initial')
devout coral
#

Oh man. Is this your first django project?

mortal mango
#

yeah

#

I'm a beginner

devout coral
#

Alright. I suggest a tutorial or something first.

mortal mango
#

yeah I read the official django guide

#

I'm probably forgetting to do something

devout coral
#

Ok, because you are running into some really silly errors no offense.

mortal mango
#

I'm not familiar with these errors

#

yeah sorry if I'm being a pain

devout coral
#

You are not

#

Just easier if you follow a tutorial to learn the basics then me explain all the errors

#

Do you like video tutorials?

#

If so follow that tutorial and you will learn a lot.

mortal mango
#

ok Thanks! I'll watch that

#

@devout coral I migrated it!

devout coral
#

@mortal mango Nice

mortal mango
#

I watched a couple minutes of the databases and migrations video and found out how to do it

devout coral
#

What happened

#

What was the issue

mortal mango
#

I had some migrations from yesterday that I had to delete

#

in the migrations folder

devout coral
#

I thought you started from scratch...

mortal mango
#

and then I did makemigrations and it worked

devout coral
#

Yeah

mortal mango
#

yeah I started yesterday and then I stopped and continued today

devout coral
#

Ok

#

So now more errors?

mortal mango
#

yeah no more

#

it finally worked!!

#

so would I do a cname for a subdomain to point to the vps or would I do an a record in the dns

devout coral
#

I would suggest an A record

#

make sure you also set up your settings.py file to allow the host

mortal mango
#

oh ok

#

I have to set up the webserver now so I can access the api with the domain

devout coral
#

Good stuff

#

Good Luck with the rest of the development

haughty turtle
#

Ok first official Django project, its a clothing store, 3 categories, so I have to create a Product model, associate this model to one of the three categories, associate a name, price, description, image and etc all to this product model.

vapid acorn
#

Why is this not working in django (its a template html-file)

{% for comment in comments%}
            <p id="comment_style">{{comment.text_content}} {{ comment.author }}</p>
            <p id="comment_style">{{ commment.author }}</p>

It cant access comment.author the second time I want to use it

#

The first p-tag is working as intended and shows all the info

#

But it somehow works if I use like a h6 tag (and probly any other tag)

devout coral
#

What is the error you are getting?

devout coral
#

Can anyone offer some help with celery and django?

peak meadow
#

Guys. To run uWsgi with Flask, do I need to setup a virtual env ? I do have a Raspberry and my uWSGI seems to be unable to import libraries 🙂

manic grail
#

I have a noob question, what would I use JS for when creating a Flask app

quick cargo
#

frontend

manic grail
#

But css and html are there for it

dapper tusk
#

there are things you can only do with JS, such as infinite scroll

mortal mango
#

@devout coral hello again, I got the webserver and everything set up but it says forbidden You don't have permission to access this resource. when I try going to the page.

#

do you know why this happens?

devout coral
#

You need to give proper perms to whatever is running your server

mortal mango
#

yeah I did sudo chown -R :www-data exo_api to give apache access to all of my files

#

@devout coral

manic grail
#

One more thing, is incorporating JS into html files made with templates (in Flask) the same as with Normal html?

devout coral
#

Not sure atm and I am leaving the office now. Sorry hope someone else can help.

mortal mango
#

oh ok

acoustic oyster
#

One more thing, is incorporating JS into html files made with templates (in Flask) the same as with Normal html?
yeah, it can work pretty much the same. I have used the <script> tag in django templates a few times and it did its job, this should be the same in flask.

mortal mango
#

I'm using django and I got an apache webserver and everything set up but it says forbidden You don't have permission to access this resource. when I try going to the page. Does anyone know why this happens?

acoustic oyster
#

if yo just deployed, make sure you have set up ALLOWED_HOSTS

#

@mortal mango

mortal mango
#

yeah I have my domain and my vps ip address

acoustic oyster
#

hmmmm, this is usually an issue with allowed hosts. This may be an actual server permission error chmod

mortal mango
#

someone told me it's because apache can't access the files

#

so what should I do with chmod?

acoustic oyster
#

yeah, you need to give apache permission, one sec

mortal mango
#

I gave apache permission with chown

acoustic oyster
#

were you following a guide or tutorial?

mortal mango
#

yeah

#

I was following this

acoustic oyster
#

hmmmmm, Im trying to remember my deploy

#

I deployed with corey schafer

mortal mango
#

did you follow the whole series?

acoustic oyster
#

you may want to try the permissions again then
sudo service apache2 reload

mortal mango
#

I didn't follow the whole series. I just watched that video to know how to deploy

acoustic oyster
#

I did follow the whole thing 😄

mortal mango
#

oh ok

sweet roost
#

anyone know if you can modify tags within a div using internal css?
I can modify the general section but cant seem to figure out how to modify each tag within specifically

mortal mango
#

@acoustic oyster in the tutorial it says to do something with the media files. I didn't do that because I don't have the media files. Is that fine?

#

I just reloaded it and it still says forbidden

acoustic oyster
#

is it forbidden on every page?

you may need to check your virtual host as well

mortal mango
#

yeah it's forbidden on every page

acoustic oyster
#

oh, double check you have the correct file paths in virtual host AND when you chmod, my project structure was a little different so I had some issues as well.

mortal mango
#

virtual host?

#

what do I do with chmod?

acoustic oyster
#

if these common solutions do not help, then you should check your apache error logs to see what exactly is happening.

#

well, follow the tutorial for chmod, but double check you are FORSURE doing the correct file paths.

And if you are doing the apache tutorial, you are probably using virtual hosts.

so, like cd /etc/apache2/sites-available and make sure your .conf is setup properly. Again QUADRUPLE check the file paths match your project

mortal mango
#

oh ok

#

would sudo chmod -R :www-data exo_api be correct?

plucky tapir
#

my partner was trying to run my django project but cant run from . import views, any idea why? I think the files are in their apprpriate folders

mortal mango
#

its from putyourapphere import views

vestal hound
#

my partner was trying to run my django project but cant run from . import views, any idea why? I think the files are in their apprpriate folders
@plucky tapir what's the error

#

and where are you importing to

mortal mango
#

@acoustic oyster so where would I check the file paths? Would it be the aliases that I added?

plucky tapir
#

@vestal hound It says ImportError: attempted relative import with no known parent package and its just the main urls.py importing from the main views.py

vestal hound
#

...?

#

show your directory structure

mortal mango
#

@acoustic oyster I don't have an html file. I'm using Django Rest Framework to build an API. Does it cause problems if I don't have an index.html file?

plucky tapir
vestal hound
#

@plucky tapir that means that you're running code from within projects itself

plucky tapir
#

@vestal hound which is fine?

vestal hound
#

@vestal hound which is fine?
@plucky tapir not if you want to use it as a package.

#

for the purpose of relative imports

#

projects is only a package from the outside.

#

so if, from the root directory, you ran from projects import views, for example, that would work.

#

and if in projects you had a from . import views, that would work too

#

unless you were running it from that directory itself, in which case it wouldn't "know" that it was a package, therefore there'd be no parent (.) to import from

acoustic oyster
#

@acoustic oyster I don't have an html file. I'm using Django Rest Framework to build an API. Does it cause problems if I don't have an index.html file?
You need to double check how your project is structured and that you included the correct files in your virtual host setup in /etc/apache2/sites-available (is where I think it should be.

You would need to double check that your setup matches the tutorial setup

plucky tapir
#

outside the projects folder?

vestal hound
#

outside the projects folder?
@plucky tapir how are you running it?

#

from terminal?

#

what's your PYTHONPATH?

plucky tapir
#

through terminal and pycharm it runs fine for me. I'm not sure what setup or IDE they're using tbh. And I'll have to look that up real quick

#

@vestal hound and one small question for user to link to database info, its currenlty linked to user, should it be to the user ID?

vestal hound
#

through terminal and pycharm it runs fine for me. I'm not sure what setup or IDE they're using tbh. And I'll have to look that up real quick
@plucky tapir pretty sure it's a path issue.

#

if you run code from within a package relative imports won't work because there's no parent package to import from.

#

and that's basically the only case in which you get that error.

#

@vestal hound and one small question for user to link to database info, its currenlty linked to user, should it be to the user ID?
@plucky tapir could you elaborate? I'm not really sure what you mean

plucky tapir
#

appreciate it.
So I'll probably have to try to get their paths in order. I didn't upload my virtual environment since it was too large, is that have anything to do with setup

Sure, right now in Django a user logs in, they use create views and input data. the models.py takes in the user as an author automatically to know who created that in the database

#

so takes in user as a foreign key

#

should it take in the user id instead of the user? For good practice

vestal hound
#

should it take in the user id instead of the user? For good practice
@plucky tapir do you mean in terms of how the related field is defined in the Model?

#

appreciate it.
So I'll probably have to try to get their paths in order. I didn't upload my virtual environment since it was too large, is that have anything to do with setup

Sure, right now in Django a user logs in, they use create views and input data. the models.py takes in the user as an author automatically to know who created that in the database
@plucky tapir perhaps. it should just work out of the box though, which makes me think they're not running it properly

#

like python manage.py runserver should not run into any problems

plucky tapir
#

@vestal hound yep I believe so, right now its author = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE, default=None)

vestal hound
#

yup

#

so ForeignKey abstracts away the idea of ID/primary key for you

#

but internally what happens is that it creates an author_id field and sets up the necessary FK constraints

#

so in fact, you'll notice that on the model you can access both author (User object) and author_id (int).

plucky tapir
#

Oh wow okay I'll have to try that out to see. Itd be django acceptable to leave it as User ?

vestal hound
#

Oh wow okay I'll have to try that out to see. Itd be django acceptable to leave it as User ?
@plucky tapir not just acceptable, but preferred

#

although I question why your FK is nullable

#

not wrong but I'm curious

plucky tapir
#

@vestal hound alright nice.

@vestal hound Tbh I had some issues at the time with the foreign key, I read through some forums and found someone added blank and null. So still learning but if it’s more conventional to switch it up I’m for it.

vestal hound
#

@vestal hound alright nice.

@vestal hound Tbh I had some issues at the time with the foreign key, I read through some forums and found someone added blank and null. So still learning but if it’s more conventional to switch it up I’m for it.
@plucky tapir IMO don't use nullable FKs unless you know what you're doing

#

there's a fair bit of theory behind this

#

which you can get into if you're interested (it'll help you design better schemata)

#

but in this specific context...would it make sense to have an instance of whatever model that is without a creator?

plucky tapir
#

@vestal hound I could learn if it’d help me out more down the line.

For now I believe so
the user is inputting info that is specific to them

cerulean otter
#
@app.before_request
def before_request():
    database.connect()

@app.after_request
def after_request(response):
    database.close()
    return response
By default, all your application needs to do is ensure that connections are closed when you are finished with them, and they will be returned to the pool. For web applications, this typically means that at the beginning of a request, you will open a connection, and when you return a response, you will close the connection.

I got this from peewee's documentation. Isn't it a bad idea to recycle a single connection per request? What if you're doing a lot of async stuff? Wouldn't it be better to pull a new connection from the connection pool everytime you do a query. Immediately after you're done you would close the connection so it goes back into the pool

#

WIth the above solution, each request will have a single connection to play with. The bad side about this is if you have a number of async db calls, all but one is going to be blocked

#

Anyone ;(. Every thing I've researched suggests that this is a bad idea

#

Oh wait there is a db channel

pine yew
#

anyone have opinions on web frameworks? Looking for things to learn besides Flask

#

I've been hearing good things about FastAPI, but I had a terrible experience with Connexion so I'm a little hesitant about things that are new

acoustic oyster
#

I am heavily biased towards django always xD but django is thicc

gaunt marlin
#

Django is good if you want a quick admin system(customize admin is hell though, not recommend), FastAPI exactly what it's name is to make API super fast(i also love the force typing in api though, from someone who worked with typescript). If you want to do from scrap Flask not a bad choice

#

in the end use what you need, not which is better

sturdy pike
#

@gaunt marlin most of the jobs are with front end / react if you have the free time to pick up additional frameworks go with react imo

#

If you can get good with vanilla css and efficient in react you're solid

#

@pine yew

gaunt marlin
#

but those are frontend framework, you always need backend frameworks for server. 2 Different stuffs

sturdy pike
#

Sorry I didn't mean to cc you I thought you were the one asking. Also, yeah I understand, however I think he just asked about a framework not a backend or front one specifically. I'm sure he's interested in backend being as this is a python discord it's just that personally I feel anyone with free time to pick up a framework who wants work, should learn something like react. Plus he said he already knows Flask. I'm a Django guy myself but I just see where the trends are for work etc. Personally you can do a lot more with django and if you already know flask, django may be the best option since there will be less of a learning curve and django can do more for you out of the box. So perhaps my ultimate suggestion would be to become as profficient as you possibly can without stopping with django. In the mean time don't look at any other frameworks unless you're already in the field then I'll stop right there

idle flame
#

What is the best way to handle object level permissions in django? hit a bit of a road block on this one.

#

or, better suited, row level permissions

#

say I have a table full of quotations, with 1 column being customer

#

and each "user" is attached to a customer

#

user being foreign key to a customer in a customer table

#

how can I ensure each user can only see rows in the quotation table, for which the foreign key is the same customer as his user.customer is?

slow briar
#

Do you guys know some good servers for help with Pyramid Framework?

vestal hound
#

What is the best way to handle object level permissions in django? hit a bit of a road block on this one.
@idle flame vanilla Django?

slow briar
#

I never understood how Django works, so I'm going with Pyramid. Can anyone recommend me some Pyramid servers?

idle flame
#

@vestal hound yes, i intend putting DRF ontop of it

#

but pretty much vanilla

#

still in early development so I can certainly rework something else into it. I've googled the issue to little avail, it seems postgres has something I can integrate to assist but im running on mysql

#

django guardian but there seems to be some concern it's not applicable to my use case

vestal hound
#

@vestal hound yes, i intend putting DRF ontop of it
@idle flame okay it depends on whether that's going to be a general thing or specific to your view

#

if the latter, then your filter should be in the queryset

idle flame
#

drf mentions object level permissions but the implementation but that only applies to api calls right?

vestal hound
#

drf mentions object level permissions but the implementation but that only applies to api calls right?
@idle flame basically, yes.

#

but where else would you perform that check?

#

like I believe for that case you want Quotation.objects.filter(customer=request.user.customer)?

idle flame
#

bare with me, I'm self teaching here and seem to have hit the limits of my basic understanding. so I'm not running an API call when on the website - only when I'm using the api itself (i'm using it for sending sensor readings into the database)

#

I also dumbed the query down a little bit - so I have user.organisation (the organisation the user belongs too)

#

but then on the other side of the query I have organisation.location.area.zone.asset.quotation

#

and other tables than quotation,

vestal hound
#

organisation.location.area.zone.asset.quotation

#

🥴

idle flame
#

exactly 🙂

vestal hound
#

okay, but that's all irrelevant

#

the thrust of the issue is that you only need to impose control at one point: when your API returns data

#

correct?

idle flame
#

its actually the opposite 😦

vestal hound
#

why?

idle flame
#

the only thing using the API is embedded software i've designed

#

so a little data collector pushing sensor readings to a database

#

then we analyse the data, make reports in a "report" table

#

and there are reports many to one asset

#

of which asset many to one zone, then area, then location then organisation

#

does it make sense :S

#

and users will need permissions for different levels of that hieracy

vestal hound
#

I mean, all that makes sense

surreal harness
#

has anyone ever used django rest framework? i'm having trouble using nested serializers. i can't get the nested models to show on my api view

vestal hound
#

has anyone ever used django rest framework? i'm having trouble using nested serializers. i can't get the nested models to show on my api view
@surreal harness what's your specific problem

#

I mean, all that makes sense
@vestal hound but ultimately, either your API does the permission checking or your embedded software does, right?

surreal harness
#

i have two models, User and UserContactInformation

#

UserContactInformation has foreignkey(user)

#

and i want to nest the contactinformation in the user model in the serializer

#

so when i go to /api/users/1 it will show the user attributes and the nested contact information

vestal hound
#

uh-huh

#

go on

surreal harness
#

so

#

let meshow some code

idle flame
#

so what I'm ultimately trying to achieve is: users cannot see or interact with assets from other users.

For my use case you recommend applying it via the queryset?

vestal hound
#

so what I'm ultimately trying to achieve is: users cannot see or interact with assets from other users.

For my use case you recommend applying it via the queryset?
@idle flame yes. why would the embedded software know about such things?

#

which is why I'm saying you should control access at one point: your API

surreal harness
#

class UserContactInformationSerializer(serializers.ModelSerializer):
  class Meta:
    model = UserContactInformation
    fields = ['id', 'user', 'contact_information']

class UserSerializer(serializers.ModelSerializer):

    user_contact_information_set = UserContactInformationSerializer(many=True, read_only=True)
    class Meta:
        model = User
        fields = ['id', 'first_name', 'last_name', 'user_contact_information_set']

apparently this is supposed to be enough to get it to show properly nested, but it just won't show the nested contact information

idle flame
#

the embedded software doesnt know or care, no permissions required. it only knows the asset_id

vestal hound
#

the embedded software doesnt know or care, no permissions required. it only knows the asset_id
@idle flame precisely, right? so it should let the API handle it.

idle flame
#

yes, the API will handle it.

vestal hound
#

class UserContactInformationSerializer(serializers.ModelSerializer):
  class Meta:
    model = UserContactInformation
    fields = ['id', 'user', 'contact_information']

class UserSerializer(serializers.ModelSerializer):

    user_contact_information_set = UserContactInformationSerializer(many=True, read_only=True)
    class Meta:
        model = User
        fields = ['id', 'first_name', 'last_name', 'user_contact_information_set']

apparently this is supposed to be enough to get it to show properly nested, but it just won't show the nested contact information
@surreal harness what do you mean "won't show"?

#

what output do you get

idle flame
#

BUT the user goes to the website and views the reports

vestal hound
#

BUT the user goes to the website and views the reports
@idle flame okay, so the embedded software has to know which user is using it

idle flame
#

no -

surreal harness
#

I only get first_name, last_name, and id without even seeing a field for user_contacT_information.

},
        {
            "id": 4,
            "last_name": null,
            "first_name": "test user",
        }
}
idle flame
#

the software sends the data in, I parse it and save it in the table on the back end side

#

data comes in via API, back end processes it and stores it in a table

vestal hound
#

the software sends the data in, I parse it and save it in the table on the back end side
@idle flame if it doesn't then it can't perform permission checking

#

because ultimately the backend has to know which user is requesting data

idle flame
#

django has to permission check

vestal hound
#

to do that, right?

#

and for the backend to know, the previous layer has to know, too

idle flame
#

Sorry - i'm a beginner here - the embedded for all purposes is a superuser

#

it handshakes and sends the data in via api. django then reads and processes this data, then stores it in the relevent table

#

the user logs into the front end of website and views this table

#

i need to restrict the user to only objects in this table that belong too him

#

because there are hundreds of embedded components

#

on 20 customers

#

in 100 different locations

vestal hound
#

i need to restrict the user to only objects in this table that belong too him
@idle flame yup, I understand that

#

but the thing is

#

the user logs into the front end of website and views this table
@idle flame you can't have validation purely on the frontend.

#

the backend has to know which user is requesting data

#

otherwise the only choice is to send all the data.

idle flame
#

ohh yes -

#

it does!

#

the default django user model

#

I extended this

#

well i one to one'd a user profile

vestal hound
#

so each request has to know the source user

#

yeah, those are details

#

but the main thing is that if each request is tied to a user

#

then the problem is trivial

idle flame
#

so when user makes a request, I look at this query, crawl around the tables linked to his to see where is view permissions end

#

then crawl the table he is trying to view and pull only things he has permission for

native tide
#

Why don't image show up on my Django app

idle flame
#

okay. I know this is a really dumb question but where is that applied?

vestal hound
#

so when user makes a request, I look at this query, crawl around the tables linked to his to see where is view permissions end
@idle flame in what form are the user's credentials transmitted?

#

okay. I know this is a really dumb question but where is that applied?
@idle flame not a dumb question, but where is what applied?

idle flame
#

its currently NOT that's what I'm trying to build - sorry beginner here. i wanted it to work via @ login required

#

and extend this to the user

#

so he needs to send his user to django and then I need to apply it from this on every page

vestal hound
#

yup

#

precisely!

#

so like

#

with Django REST framework

#

each view takes a request, right

#

and if you set up your authentication backend properly

idle flame
#

yes

vestal hound
#

you can just access request.user

#

and that will be a User object representing the user asking for data.

#

then you can just query the Quotation table with the filter I mentioned above

#

Quotation.objects.filter(customer=request.user.customer)

idle flame
#

at the moment I have only the django base authentication back end and I dont know how to extend it and apply it

cosmic aspen
#

a good book to learn django please ??

idle flame
#

i'm alot further forward so far, sorry @vestal hound this is alot more helpful than it seems. So basically I need to google "extending django authentication back end" instead of "how to enforce object level permission django??"

vestal hound
#

i'm alot further forward so far, sorry @vestal hound this is alot more helpful than it seems. So basically I need to google "extending django authentication back end" instead of "how to enforce object level permission django??"
@idle flame DRF comes with several authentication backends

#

you can look into those and see if they fit your usecase

#

so that's one part of the problem

#

once you handle that, the rest should be easy

#

because it'll take care of populating the user attribute of the request object

#

that's one part.

#

but do you understand

#

what I mean by this?

idle flame
#

but that will only apply to API calls right? not

vestal hound
#

then you can just query the Quotation table with the filter I mentioned above
@vestal hound this

idle flame
#

wait

vestal hound
#

but that will only apply to API calls right? not
@idle flame yes, but the backend only interacts with the outside through API calls, right

idle flame
#

I could change my front end to route via the API though

#

like with vue

surreal harness
#

a good book to learn django please ??
@cosmic aspen try looking at this book called two scoops of django

vestal hound
#

I could change my front end to route via the API though
@idle flame yes, but that's a separate layer

#

the main point is that the data ultimately comes from the backend

idle flame
#

but DRF sits ontop of the back end though? doesnt a standard web browser going to the website

#

avoid this

vestal hound
#

no

#

user -> website -> backend, yes?

idle flame
#

omg. thankyou.

#

tes

#

yes

vestal hound
#

otherwise how do they get their data

#

everything has to go through the backend

idle flame
#

so it goes website ->drf -> backend??

vestal hound
#

DRF is the backend

#

also, I think you might be a bit confused regarding object-level permissions

#

basically, normal permissions: "can this user access this view?"

#

object-level permissions: "this user can access this view, but can they also access this object?"

idle flame
#

oh shit

#

yes I am

#

i missed this

vestal hound
#

which is kind of neither here nor there, but just a point

idle flame
#

I'm worried that if I make the view it's going to show them everything

#

(which it is right now)

vestal hound
#

because object-level permissions are more something you do when you want to get a single object

idle flame
#

that is the bit I'm trying to fix

vestal hound
#

in this case it's a question more of filtering a number of results

idle flame
#

okay, I think I need to work DRF in and read it's docs

#

do you mind if I work on this for a while and message you later?

surreal harness
#

if you have time @vestal hound can you help me with the issue i was talking about above

vestal hound
#

you can just ask here and whomever will respond

#

if you have time @vestal hound can you help me with the issue i was talking about above
@surreal harness wups sorry I entirely forgot

#

my bad

surreal harness
#
class UserContactInformationSerializer(serializers.ModelSerializer):
  class Meta:
    model = UserContactInformation
    fields = ['id', 'user', 'contact_information']

class UserSerializer(serializers.ModelSerializer):

    user_contact_information_set = UserContactInformationSerializer(many=True, read_only=True)
    class Meta:
        model = User
        fields = ['id', 'first_name', 'last_name', 'user_contact_information_set']

results are just

},
        {
            "id": 4,
            "last_name": null,
            "first_name": "test user",
        }
}
vestal hound
#

can I see your models

surreal harness
#

yeah

vestal hound
#

User and UserContactInformation

surreal harness
#

well okay, I sort of reduced the complexity to make it easier to write out

#

the actual model is like

vestal hound
#

and also your view

surreal harness
#

models.py

class CandidateContact(models.Model):
    class Meta():
        db_table = 'candidate_contact'

    candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
    contact = models.ForeignKey(
        ContactType, null=True, on_delete=models.SET_NULL)
    contact_detail = models.CharField(max_length=255)

class Candidate(models.Model):
    # TODO: Confirm usage of email/phone with the mobile developers as identifier, as opposed to UUID
    class Meta():
        db_table = 'candidate'

    id = models.AutoField(primary_key=True)
    last_name = models.CharField(max_length=255, blank=True, null=True)
    first_name = models.CharField(max_length=255)
    gender = models.ForeignKey(
        Gender, default=3, null=True, on_delete=models.SET_DEFAULT)
    country = models.ForeignKey(
        Country, verbose_name="nationality", null=True, on_delete=models.SET_NULL)

serializers.py

class CandidateContactSerializer(serializers.ModelSerializer):
    class Meta:
        model = CandidateContact
        fields = [
            'id',
            'candidate',
            'contact_detail',
            'contact'
        ]

class CandidateSerializer(serializers.ModelSerializer):
    
    # Retrieve connected fields 
    candidate_contacts = CandidateContactSerializer(
        many=True, 
        read_only=True,
    )

    class Meta:
        model = Candidate
        fields = [
            'id',
            'last_name',
            'first_name',
            'gender',
            'country',
            'candidate_contacts',
        ]

#

views.py

@api_view(['GET', ])
def candidate_detail_view(request, id):
    try:
        candidate = Candidate.objects.get(id=id)
    except Candidate.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == "GET":
        serializer = CandidateSerializer(candidate)
        return Response(serializer.data)


# Try class based view

class CandidateListView(ListAPIView):
    queryset = Candidate.objects.all()
    serializer_class = CandidateSerializer
    # authentication_classes = (TokenAuthentication)
    # permission_classes = (IsAuthenticated)
    pagination_class = PageNumberPagination
vestal hound
#

I suppose Candidate corresponds to User?

surreal harness
#

no, its a database filled with some master data

#

a bunch of people previously registered

#

to some other service

vestal hound
#

no, I mean, in your example above

surreal harness
#

oh yes

#

this is the view on /api/candidate/1

HTTP 200 OK
Allow: OPTIONS, GET
Content-Type: application/json
Vary: Accept

{
    "id": 1,
    "last_name": null,
    "first_name": "Võ Minh Luân",
    "gender": 3,
    "country": 235,
    "education_in_japan": false
}
#

as you can see, candidate contact isn't showing up at all. i'm wondering if its because i'm not defining the relationship correctly

vestal hound
#

that's your function-based view, right?

surreal harness
#

yes

#

in the list based view it doesn't show up either

vestal hound
#

weird my internet isn't working at all

#

I want to Google something but I can't

#

sec

surreal harness
#

ok

unborn dust
#

has anyone used python-social-auth with django?

vestal hound
#

ok
@surreal harness okay this shouldn't be happening

surreal harness
#

i agree

#

HAHA

vestal hound
#

try deleting all your .pyc files?

#

that's my best guess

#

there's only one CandidateSerializer, right

#

like you're not importing one that doesn't have that field

surreal harness
#

nope

#

as in yes, there's only one

#

i have a feeling that

#

something is messed up with the relation

vestal hound
#

it looks fine to me

surreal harness
#

so we know that CandidateContact uses Candidate as a foreign key

vestal hound
#

although

surreal harness
#

but is the serializer doing that

vestal hound
#

if you have defined your models in that order

#

that shouldn't work

surreal harness
#

should it matter? the database exists

vestal hound
#

because Candidate doesn't exist when CandidateContact is defined

#
class CandidateContact(models.Model):
    class Meta():
        db_table = 'candidate_contact'

    candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
surreal harness
vestal hound
#

purely as a matter of Python

#

at this point, Candidate is not yet defined, right

#

like this shouldn't cause that problem, but just a point

surreal harness
#

right true, well in the models Candidate is defined first

vestal hound
#

anyway, that is more or less how you should define a nested field, so I don't think it's that

surreal harness
#

in the serializers CandidateContact needs to be defined first since Candidate depends on it, I think

surreal harness
#

yeah I followed this guide exactly, but didn't seem to work

#

came across something that said that query_sets retrieved by the serializers have a default naming convention of having the word set appended to them

#

however even when adding the word set, as in 'candidate_contact_set' and having the fields include 'candidate_contact_set' instead of just 'candidate_contact', same result

vestal hound
#

came across something that said that query_sets retrieved by the serializers have a default naming convention of having the word set appended to them
@surreal harness this is true

#

but it shouldn't matter

surreal harness
#

interestingly enough applying depth=1 inside of the Meta class for the Candidate model automatically nests stuff like gender, etc.

vestal hound
#

oh wait actually

#

shouldn't the name be candidate_contacts

surreal harness
#

does it have to do with the name?

#

yeah it is candidate_contacts

vestal hound
#

I'm actually not sure how this works because I always set custom related_name

surreal harness
#

I see

vestal hound
#

let me try

surreal harness
#
class CandidateSerializer(serializers.ModelSerializer):
    
    # Retrieve connected fields 
    candidate_contacts = CandidateContactSerializer(
        many=True, 
        read_only=True,
    )

    class Meta:
        model = Candidate
        fields = [
            'id',
            'last_name',
            'first_name',
            'gender',
            'country',
            'candidate_contacts',
        ]
#

is what it is currently

vestal hound
#

okay can you try this

#

in CandidateContact.candidate, set related_name to "candidate_contacts"

#

so like models.ForeignKey(Candidate, related_name='candidate_contacts', on_delete=models.CASCADE)

#

this is my best guess

surreal harness
#

does that require a migration

#

ah it does. give me a second

#

does it being plural actually make a difference

vestal hound
#

my thought process is

#

if the attribute name is wrong, it can't find the attribute representing the related objects, therefore it assumes that it's null

#

does it work

surreal harness
#

about to test

#

i just have to deploy it and shit, it takes a second

#

yeah, that worked

vestal hound
#

okay

#

so I guess my mental model was accurate

#

your serializer field name needs to correspond to the related object manager

surreal harness
#

do i actually need to define these related names

#

what are they by default?

full steppe
#

can flask be used for front end web dev

surreal harness
#

If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.map_set.all().

#

this is what i see, however i distinctly remember trying this

#

let me try again

vestal hound
#

can flask be used for front end web dev
@full steppe NO

#

no

#

caps

full steppe
#

k

surreal harness
#

yeah, so when trying to add another relation it seems like this thing they say is actually not true

#

i have some other related fields and simply appending 'set' to the field name doesn't do anything

vestal hound
#

uh

#

I am actually not sure about this, because like I said, I always use custom related_name

#

which IMO is a good practice

surreal harness
#

okay, i just don't really know what it represents conceptually

vestal hound
#

okay

humble elbow
#

hello

vestal hound
#

so like

#

candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)

#

each ContactInfo has a Candidate

humble elbow
#

how can i log in to my robot

vestal hound
#

which you can access with, say, contact_info.candidate

#

but since it's a foreign key (one to many relationship)

surreal harness
#

yep

vestal hound
#

each Candidate has many ContactInfos

#

so candidate.candidate_contacts.all() is the QuerySet of ContactInfos corresponding to that Candidate

#

where candidate_contacts is the related_name you set

surreal harness
#

is candidate.candidate_contacts.all() even possible

vestal hound
#

yes

#

you can filter it just like any other QuerySet

surreal harness
#

right, but from the candidate model perspective no relationship is set

#

that only exists in the contactinfo model

vestal hound
#

it's more or less the same as ContactInfo.objects.filter(id=candidate_id)

#

no

#

it is

surreal harness
#

i see

vestal hound
#

relationships are always two-way

#

in this context

surreal harness
#

ok, got it

#

the thing is

#

candidate.candidate_contacts.all()

#

that candidate_contacts part, that name corresponds to what exactly

vestal hound
#

the related_name

surreal harness
#

RIGHT

vestal hound
#

if you change related_name to monkeys it'll be candidate.monkeys.all()

surreal harness
#

ok now i really get it

#

oh ididn't see th elast sentence of what you wrote is why

#

and basically if I never set that name it should default to something

#

which i assumed was just the model name defined with underscores and appended with 'set'

#

but i guess that's not true

#

but now i know how to check

vestal hound
#

it should have been

#

maybe there was a typo or something

surreal harness
#

what happens i think is that

#

if you define a model like

#

camel case, it will not apply underscores

vestal hound
#

it's convention

#

to use upper camel case

surreal harness
#

so it would just be candidatecontactinformation_set

vestal hound
#

for models

surreal harness
#

right

#

but the default related name evaluates to the modelname

vestal hound
#

in this case it should be contactinfo_set

surreal harness
#

yeah

#

i was looking for contact_info_set was my issue

vestal hound
#

oh

#

yeah that's wrong

trail axle
#

is Flask or Django more beginner friendly?

slow briar
#

Flask is beginner friendly.

#

I had a question guys, how do you know the user in Pyramid?

fickle fox
#

guys what is good module to work with pdf files in flask? Like to render them??

#

and as well can someone tell me where can i find good tutorial to save pdf files and images i want thme stored in database

outer pier
#

anybody knows fcm django ?

gaunt marlin
#

@fickle fox PDF and image are file so you only need to handle store file to database here a good tutorial https://www.youtube.com/watch?v=TLgVEBuQURA&ab_channel=PrettyPrinted

Uploading files in Flask to a database is very simple to accomplish. In this video, I'll show you to use an HTML upload form to upload files to Flask and save them to blob columns in your database using SQLAlchemy. The example database I'm using is SQLite, but most popular dat...

▶ Play video
fickle fox
#

oh thanks

#

i am following that guide

#

tho i found some errors in my code qwq

#

oh never mind i fixed it hahahah

surreal harness
#

how could i implement a count_only query parameter in the rest framework that would only count the total number of results without paginating them?

gaunt marlin
#

@surreal harness use .count() at the end of the query it will return the number instead

#

in your view query

surreal harness
#

hm...looks like that might be incompatible with the pagination class

mortal mango
#
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.
```I'm getting internet server error when I try running my django app with apache2. Does anyone know why?
glacial quartz
#

hi i want to make a game backend using websockts, should i use Hypercorn or what for it? basicly i just want 1 singleton for game and to pass data and close connections when needed

#

does Hypercorn run on arm, am asking cuz i read there is meybe some cython stuff

fickle fox
#

ok guys i managed to save image to database but now how can i display it on website

peak meadow
#

Guys. I'm fine to run my Flask app only with Werkzeug if I'll get only requests from the same computer at most 20 times a day ? I have read that it's able only to hold 1 request at once, my flask doesn't have any static files because it's an API

finite cape
#

I've been working on a django restframework project with the aim of generating json files out of my model objects like the following :

“label”: <label>},...]
```
here's the models implementation 
```py

from django.db import models
#Document Model
class Document(models.Model):
    text = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.text[:50]
#Label Model
class Label(models.Model):
    value = models.CharField(max_length=100)
    background_color = models.CharField(max_length=7, default='#209cee')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.value

class SequenceAnnotation(models.Model):
    document = models.ForeignKey(Document, related_name='LabellizedDocuments',null=True, blank=True, on_delete=models.CASCADE)
    label = models.ForeignKey(Label,null=True, blank=True, on_delete=models.CASCADE)
    text = models.CharField(max_length=10000)
    text_color = models.CharField(max_length=7, default='#ffffff')
    start_offset = models.IntegerField()
    end_offset = models.IntegerField()
    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['start_offset','end_offset', 'label'], name='existing-annotation')
        ]


    def clean(self):
        if self.start_offset >= self.end_offset:
            raise ValidationError('start_offset is after end_offset')
    def __str__(self):
        return str(self.text)
```
lime ibex
#

hey, is there a way in flask to hide the url parameters that are shown when i try to login

quick cargo
#

no

pine yew
#

@sturdy pike I'm quite experienced with Flask and web tech. I was looking for a new backend framework to learn and have been exploring asyncio lately.

native tide
#

which webdriver should I download to run selenium on Edge browser?

swift sky
#

having an issue displaying sql query content into a table

#

it pulls the query correctly but not displaying it in my html

#
                             <table>
                                 <thead>
                                     <tr>
                                        <th>ID</th>
                                        <th>Load Date</th>
                                        <th>Content Type</th>
                                        <th>Content</th>
                                     <tr/>
                                 </thead>
                                 <tbody>
                                    {% for item in data %}
                                    <tr>
                                        <td>{{item[1]}}</td>
                                    </tr>
                                    {% endfor %}
                                 </tbody>
                             </table>```
#

the function:

#
   def staffdashboard():
        connection = db_connection_content()
        cursor = connection.cursor()
        sql_query = "SELECT * FROM roll_call"
        cursor.execute(sql_query)
        data = cursor.fetchmany(1)
        print(data)
        connection.close()
        return render_template("staffdashboard.xhtml", data=data, dict_to_be_passed=parse_311_sitemap(load_articles()))
#

it's displaying the th portion of the code, but not the content of the query

devout coral
#

@mortal mango Did you get that error figured out?

swift sky
#

when i inspect the html while it's running

devout coral
#

@swift sky have you done a print in staffdashboard() to ensure that data has something?

swift sky
#

there are no elements from my query present

#

so i think it's a jinja2 issue

#

@swift sky have you done a print in staffdashboard() to ensure that data has something?
@devout coral yes it pulls the correct information

mortal mango
#

@devout coral yeah I did but now I have Internal Server Error

devout coral
#

Ok. could you put the following code for me in that stafffunctionboard() function and show me the result. @swift sky

for item in data:
    print(type(item))
#

@mortal mango Do you have debugging set to false?

swift sky
#

it's a dict

#

<class 'dict'>

mortal mango
#

yeah I have debug set to false

#

should I turn it on?

devout coral
#

@swift sky ok now do this for me please:

for item in data:
    print(item)
swift sky
#

yeah but it's gonna be different data every time

#

also, I already have a dictionary being passed in my view function

devout coral
#

@mortal mango In development you should keep it on yes. But you should not be developing in a public server.

pine yew
#

@swift sky you're iterating a dict wrong

swift sky
devout coral
#

@swift sky so do the following:

{% for key, values in data.items() %}
    <tr>
        <td> {{ value }} </td>
        <td {{ key }} </td>
    </tr>
{% endfor %}
pine yew
#
<tbody>
  {% for item in data.items() %}
    <tr>
      <td>{{item[1]}}</td>
    </tr>
  {% endfor %}
</tbody>
   def staffdashboard():
        connection = db_connection_content()
        cursor = connection.cursor()
        sql_query = "SELECT * FROM roll_call"
        cursor.execute(sql_query)
        data = cursor.fetchmany(1)
        print(data)
        connection.close()
        return render_template("staffdashboard.xhtml", data=data, dict_to_be_passed=parse_311_sitemap(load_articles()))
mortal mango
#

@mortal mango In development you should keep it on yes. But you should not be developing in a public server.
@devout coral I'm on my VPS

#

I'll transfer the files to my computer and test on my computer and then push it to my VPS

pine yew
#

@devout coral you can't unpack a k,v from a dict, you need .items()

devout coral
#

@pine yew I know, that is why I edited my thing. I missed the .items()

pine yew
#

didn't see your edit - was typing 😛

devout coral
#

@mortal mango I mean it does not seem like you care too much about security so just do it on the server.

swift sky
#

bruh

devout coral
#

@pine yew Lol, yeah I also missed a few closing tags for the html

swift sky
#
<tbody>
  {% for item in data.items() %}
    <tr>
      <td>{{item[1]}}</td>
    </tr>
  {% endfor %}
</tbody>
   def staffdashboard():
        connection = db_connection_content()
        cursor = connection.cursor()
        sql_query = "SELECT * FROM roll_call"
        cursor.execute(sql_query)
        data = cursor.fetchmany(1)
        print(data)
        connection.close()
        return render_template("staffdashboard.xhtml", data=data, dict_to_be_passed=parse_311_sitemap(load_articles()))

@pine yew i did this one

pine yew
#

data = cursor.fetchmany(1) do you know what fetchmany returns?

mortal mango
#

@mortal mango I mean it does not seem like you care too much about security so just do it on the server.
@devout coral yeah I'm not sure how to configure security stuff so yeah

pine yew
#

it should be an iterator over db rows

swift sky
#

i actually chagned it to fetch all

devout coral
#

@mortal mango I mean, it does not matter at this point.

mortal mango
#

yeah ok

swift sky
#

but i havent decided, I'd probably need like the last 3 entries or something

pine yew
#

@swift sky what does your schema look like?

mortal mango
#

so do you know why it says internal server error?

swift sky
#

if by schema you mean the dict keys

devout coral
#

@mortal mango Just put debbuging to true in your settings.py and you will get a traceback

swift sky
#
    content_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    load_date = db.Column(db.DateTime)
    content_type = db.Column(db.String(20))
    content = db.Column(db.String(10000))``` @pine yew
pine yew
#

oh you're using an ORM? why are you accessing a DB query directly?

#

there's a lot of WTFs going on right now lol

swift sky
#

thanks

pine yew
#

are you using sqlalchemy then?

swift sky
#

yes

#

but i have to use pymysql because im on a mac

pine yew
#

yeah, what driver you use shouldn't matter

#

you can just access the model, that's the whole point of the ORM

swift sky
#

ahh ok

pine yew
#

is your class name Content?

swift sky
#

yes

pine yew
#

are you using Flask?

swift sky
#

yes

pine yew
#

Flask-sqlalchemy?

swift sky
#

yes

pine yew
#

OK! So here goes

#
content = Content.query.all()

pass that into your render and access the class names as expected

#
{% for item in content %}

item.content_id
#

etc

swift sky
#

one second

#
{% for item in content %}
<tr>                     
    <td>{{item.content_id}}</td> 
</tr>                    
{% endfor %}             ```?
pine yew
#

right

#

and you would pass that into your render_template call as content=content

swift sky
#

hmmm well

#

is there a way to do this without the orm

#

because i have some import errors to deal with

#

and thas a major headache

#

@pine yew

pine yew
#

@swift sky you could use the cursor like you were, but what's your headache with the orm?

swift sky
#

I have to refactor my code

pine yew
#

ah, I see

swift sky
#

i have issues with imports

#

I guess that's why I was doing it directly

#
    {% for item in data.items() %}
jinja2.exceptions.UndefinedError: 'list object' has no attribute 'items'
pine yew
#

give me a sec

#

I'm writing a PoC

swift sky
#

no worries

pine yew
#

OH

#

I see your problem

#

print(data) is consuming your iterator

swift sky
#

ill get rid of

#

it

pine yew
#

you're also closing your connection

#

before retrieving your data

swift sky
#

what do you mean?

#

you fetch it then close the connection

#

then you render the template with the data you fetched

pine yew
#

so I don't know how pymysql does it, but most DBAPI implementations will close your connection when it leaves scope automatically

#

the fetch calls are generators

#

they don't do anything until you consume them

swift sky
#

the reason i added the close connection call was because my db would overload otherwise

#

it would keep them open

pine yew
#

this is why you use an orm lol

#

it handles the minutiae for you

swift sky
#

first time flask app

#

learning a lot

pine yew
#

yeah, flask gives you the bare minimum then tells you to find some extensions

swift sky
#

well i removed the print

#

same error

pine yew
#

that error being?

swift sky
#
    {% for item in data.items() %}
jinja2.exceptions.UndefinedError: 'list object' has no attribute 'items'
pine yew
#

well you're not dealing with a dict when you use a cursor

#

you're dealing with rows of a db

#

which is an iterator of tuples

swift sky
#

yeah from what I googled some stack overflow post defined the data in a dict and then explicitly called the key using dotstring

pine yew
#

if you want to access your rows as a dict, you need a DictCursor

swift sky
#

i do actually

#
    def db_connection_content():
        return pymysql.connect(host=os.environ.get('HOST'),
                      user=os.environ.get('PYMSQL_USER'),
                      password=os.environ.get('PASSWORD'),
                      db='content',
                      charset='utf8',
                      cursorclass=pymysql.cursors.DictCursor)```
winter spindle
#

hey

swift sky
#

i guess then it depends how im connecting to the db?

#

wether it's through the ORM or through pymsql

#

.>

winter spindle
pine yew
#

@winter spindle no, you need a grid. flexbox is 2D

winter spindle
#

ohh okay then thanks

pine yew
#

er, 1D?

#

if the box of four is one element

#

then you could use a flexbox

#

but that would be very straightforward in grid

swift sky
#

oh nvm

#

i googled some stuff

#

cursor = connection.cursor(pymysql.cursors.DictCursor)

#

lets see

pine yew
#

@swift sky


def staff_dashboard():
    conn = db_connection_content()
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM roll_call')
    data = cursor.fetchall()

    try:
        fragment = render_template('''
            {% for item in data %}
              <tr>
                <td>{{ item[1] }}</td>
              </tr>
            {% endfor %}
        ''', data=data)

        return render_template([..])
    finally:
        cursor.close()
        conn.close()
#

you can also use the name instead

#

item['content'] or whatever

swift sky
#

uhh

#

i never used a fragment before

pine yew
#

it's just another thing you inject into your template

swift sky
#

it goes here?

#

return render_template([..])

pine yew
#

yeah

swift sky
#

how does it know to place in the []

pine yew
#

so instead of

<tbody>
  {% for item in data.items() %}
    <tr>
      <td>{{item[1]}}</td>
    </tr>
  {% endfor %}
</tbody>

you have

<tbody>{{ fragment }}</tbody>
#

that's just a placeholder, you put your other arguments there as usual

#

I was being lazy

swift sky
#

uhh

#

wtf tho

#

i still have to place the html refference in there

pine yew
#

yeah

swift sky
#

as well as any other args

#
     try:
            fragment = render_template('''
            {% for item in data %}
              <tr>
                <td>{{ item['content'] }}</td>
              </tr>
            {% endfor %}
            ''', data = data)

            return render_template(["staffdashboard.xhtml", dict_to_be_passed=parse_311_sitemap(load_articles())])
        finally:
            cursor.close()
            connection.close()```
#

but it's not accepting dict_to_be_passed= as a kwarg anymore

#

or positional, whichever it is

pine yew
#

no, get rid of the brackets

#

[]

swift sky
#

oh ok

pine yew
#

return render_template("staffdashboard.xhtml", dict_to_be_passed=parse_311_sitemap(load_articles()), fragment=fragment)

#

you'll need to change your html

swift sky
#

testing

#

some errors

#
    ''', data = data)
#
line 89, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: 
            {% for item in data %}
              <tr>
                <td>{{ item['content'] }}</td>
              </tr>
            {% endfor %}
#

im gonna try with just the [1]

pine yew
#

I think you need render_template_string

#

I forgot render_template expects a file

swift sky
#

unresolved refference

#

render_template_string

#

googling

pine yew
#

you need to import it from flask

swift sky
#

ok progress

pine yew
#

did you pass it into the template?

swift sky
#
<table>
    <thead>
        <tr>
           <th>ID</th>
           <th>Load Date</th>
           <th>Content Type</th>
           <th>Content</th>
        <tr/>
    </thead>
    <tbody>
       {% for item in data.items() %}
       <tr>
           <td>{{ fragment }}</td>
       </tr>
       {% endfor %}
    </tbody>
</table>```
pine yew
#

oh no

#

Get rid of the tbody content entirely

#

just replace it with {{ fragment }} like I suggested above

swift sky
#
<table>
    <thead>
        <tr>
           <th>ID</th>
           <th>Load Date</th>
           <th>Content Type</th>
           <th>Content</th>
        <tr/>
    </thead>
           <td>{{ fragment }}</td>
</table>```
#

well

#

i managed to navigate to the site

#

without the server errors

#

however

#

ID    Load Date    Content Type    Content
<tr> <td></td> </tr>
#

that is what was rendered

#

im gonna get rid of <thead> <tr> <th>ID</th> <th>Load Date</th> <th>Content Type</th> <th>Content</th> <tr/> </thead>

#

uhh

#

it just shows up as ```Roll Call

<tr> <td></td> </tr>

#

so these are not being passed i think

#

<td>{{ fragment }}</td>

#

alrighty

#

getting closer

#

<tr> <td>testing some content</td> </tr>
#

thank you @pine yew

mortal mango
#

@devout coral I turned on debug and it's still saying internal server error

devout coral
#

@mortal mango Then it might be something with your server set up. When does the error occur or how does it happen?

mortal mango
#

wait no nvm

#

it didn't save for some reason

devout coral
#

ok

mortal mango
#

let me try running the server again

peak meadow
#

Guys. I'm fine to run my Flask app only with Werkzeug if I'll get only requests from the same computer at most 20 times a day ? I have read that it's able only to hold 1 request at once, my flask doesn't have any static files because it's an API ?

urban stone
#

Guys anyone can help me with django

abstract kite
#

@peak meadow not a good idea if you don't want the app from randomly crashing or giving bad outputs
Try small webserver like gunicorn or something, they are very easy to setup

peak meadow
#

Hmm, it's a simple API, the input matters, the output is just a string like "succes"

#

@abstract kite

abstract kite
#

@peak meadow ~~still i would recommend a webserver, try this, hardly 2 commands are required to set it up 😅
https://gunicorn.org/~~

mortal mango
#

@devout coral it still says internal server error

peak meadow
#

Sure. I will try gunicorn then, thank you @abstract kite

devout coral
#

@mortal mango So it is something with your server then. When does the error occur or how does it happen?

toxic flame
#

how do you remove QuerySet from a query set selection for template rendering?

#

nvm

mortal mango
pine yew
#

@abstract kite what are you even talking about

#

@peak meadow the werkzeug web server is fine for development purposes locally

abstract kite
#

@abstract kite what are you even talking about
@pine yew ?

#

Eh my bad, I read it as same network and not same computer. I think I should go get some sleep

mortal mango
#

@devout coral Do you know why this is happening? I've had this error since yesterday night. I'm pretty sure I have my .conf set up properly.

devout coral
#

You running on Ubuntu? With Apache 2?

mortal mango
#

yeah

devout coral
#

Have you looked at the logs?

#

For Apache

mortal mango
#

yes

devout coral
#

And what do they say?

#

Do you not have a requirements.txt file which you installed all your modules from?

mortal mango
#

no, I manually installed everything

devout coral
#

Well you missed a module. Called encodings

mortal mango
#

oh

#

so sudo apt-get install encodings?

#

or pip?

devout coral
#

No, you need to download it to your virtual environment

#

Yes the latter

mortal mango
#

oh so pip install encodings

devout coral
#

Yes

mortal mango
#

it says 404 client error

devout coral
#

What does the server?

#

Did you turn on debugging

mortal mango
#

yes

#

this is the full error and traceback

devout coral
#

I suggest you go and find the package in pypi and just copy the download command

mortal mango
#

ok I'll do that

#

do you know the package name?

#

there's a bunch of packages with the word encodings in it

#

@devout coral

devout coral
#

I don't even know what your project is... Let alone a specific package you are using.

#

My guess is that it is a dependency for one of your other packages.

glacial quartz
#

hmm this

#

Quart

#

on Hypercorn

#

does it multythread, can i share a global class?

#

like can i have a global class mygame that evry connection can access?

mortal mango
#

My guess is that it is a dependency for one of your other packages.
@devout coral I haven't done anything. Pretty much all of my code is default.

glacial quartz
#

guess time to test

devout coral
#

@mortal mango What do you mean "all of my code is default"?

mortal mango
devout coral
#

Well i am not sure...

#

But by the looks of it your venv is missing something.

mortal mango
#

oh ok

devout coral
#

As I suggested yesterday. You should first develop your application in a test environment and then later deploy it. Because developing in a "production" environment just adds more variables for when things go wrong.

mortal mango
#

but I would need apache on my computer then, right?

devout coral
#

No. Django has a built in test server.

mortal mango
#

oh ok, I'll test that then

devout coral
#

You don't need to set up apache or anything like that. you just do python manage.py runserver

#

And if you are using an IDE, like pycharm, the chances of it telling you exactly which package is missing is high.

mortal mango
#

oh ok. I use VScode.

#

but is there a way I can get my secret key

#

I put it in a config.json and I tried opening it but theres nothing inside it

#

it seems to not have saved

devout coral
#

Just generate a new one.

mortal mango
#

how do I do that?

devout coral
#
mortal mango
#

what does a secret key do?

#

everything's working

devout coral
#

It is a salt and is used to generate hashes

#

Ok cool

mortal mango
#

so it's something wrong with my apache config right?

devout coral
#

So no more errors? You were just missing your secret key?

mortal mango
#

yeah I just tested it on my computer and it works fine

#

I didn't change anything

devout coral
#

Wait, it is working in your development server and not on the server?

mortal mango
#

yeah

#

it's working on my computer but it's not working on my vps

devout coral
#

ok, just do a quick pip freeze > requirements.txt

#

Then send that up to your server

mortal mango
#

I don't have a virtual environment on my computer

devout coral
#

Then on your server do pip install requirements.txt

#

@mortal mango ... Why?..

mortal mango
#

I use the same version of python everywhere so I don't use them

#

I'll start using them

#

I've never thought I need them so I didn't

#

but I'll just pip freeze everything on my computer

devout coral
#

Ok, from this day forward please use Virtual Environments. Otherwise if you work with different versions of packages you will run into issues

#

@mortal mango Do not pip freeze everything

mortal mango
#

ok I will

devout coral
#

Unless this is the only project you ever worked on with third party libraries

mortal mango
#

I don't have a lot

devout coral
#

I still do not suggest it. Then you will get random packages in your project. Just make a new venv just for this project.

mortal mango
#

ok

#

it's pip install venv right?

devout coral
#

Should be yes

mortal mango
#

it can't find it

#

do you know what the version is?

devout coral
#

virtualenv

#

That is the name mb

mortal mango
#

oh I already have it installed

#

how do I activate it?

devout coral
#

Did you just download the venv files from your server?

mortal mango
#

no

#

actually I have the venv folder on my computer

devout coral
#

So then you will have to create the environment. However, I think VS code has a thing to create it for you

#

Take a look at that

#

I do not use VS code so I cannot help too much with that.

#

Once you got it though let me know

mortal mango
#

ok

#

I don't think that makes an environment

#

I think it just tells you how to change interpreters and stuff

#

if I move the virtualenv.py file into my exo_api folder, and then I do python virtualenv.py will it activate the virtual environment inside that folder?

devout coral
#

That is not how you activate the environment no.

mortal mango
#

oh

devout coral
#

I think it is source path_to_activate_script

mortal mango
#

in the cmd window in my directory?

devout coral
#

I would suggest you put the venv folder in the root of your django project along with the mana.py file

#

Then cd into the root folder of your django folder and do the activation there

mortal mango
#

I already have a venv folder

#

from my vps

devout coral
#

Well I would suggest to make a new one. Then set up vs code for that to be the interpreter for that project.

mortal mango
#

so delete the one I have right now?

#

I would suggest you put the venv folder in the root of your django project along with the mana.py file
@devout coral do I make a new folder called venv or is the venv folder somewhere?

devout coral
#

The one in your computer yes.

#

The simplest way is just to make your venv with vs code...

#

@mortal mango

mortal mango
#

oh ok

#

I have my venv selected

#

so do I have to do all my cmd commands in vscode for the virtual environment to work?

#

@devout coral

devout coral
#

Is your venv activated now in vs code?

#

There should be a terminal or something

mortal mango
#

yeah I have a terminal

#

so now do I just run it and install everything when it pops up an error

devout coral
#

just try to run the server now through that terminal

mortal mango
#

it worked

#

so now do I push it to my vps?

#

wait so how did it work?

#

I didn't install anything

#

oh wait nvm

devout coral
#

No, now do the pip freeze